组建一支足球队
Setting up a Football Team
你好,我有问题。我的代码适用于所有号码,但我不知道当我发送它时,我得到了错误的答案。这里是问题的链接。任何帮助都可能非常完整http://sharecode.ir/section/problemset/problem/2499
#include <iostream>
#include <string>
#include <cstring>
#include <cstdio>
using namespace std;
int main()
{
while(1)
{
int pos[3];
for(int i=0 ; i<3 ; i++)
{
cin>>pos[i];
}
if(pos[0] == 0 && pos[1] == 0 && pos[2] == 0)
return 0;
if(pos[0]<=26)
{
if(pos[1]<60)
{
cout<<"No positions"<<endl;
continue;
}
if(pos[1]>=60 && pos[1]<70)
{
if(pos[2]<200)
{
cout<<"No positions"<<endl;
continue;
}
else
{
cout<<"Forward"<<endl;
continue;
}
}
if(pos[1]>=70 && pos[1]<80)
{
if(pos[2]<200)
{
cout<<"No positions"<<endl;
continue;
}
if(pos[2]>=200 && pos[2]<500)
{
cout<<"Forward"<<endl;
continue;
}
if(pos[2]>=500)
{
cout<<"Mid-field Forward"<<endl;
continue;
}
}
if(pos[1]>=80)
{
if(pos[2]<200)
{
cout<<"No positions"<<endl;
continue;
}
if(pos[2]>=200 && pos[2]<300)
{
cout<<"Forward"<<endl;
continue;
}
if(pos[2]>=300 && pos[2]<500)
{
cout<<"Forward Defense"<<endl;
}
if(pos[2]>=500)
{
cout<<"Mid-field Forward Defense"<<endl;
continue;
}
}
}
if(pos[0]>26 && pos[0]<=30)
{
if(pos[1]<80 && pos[1]>=70)
{
if(pos[2]>=500)
{
cout<<"Mid-field"<<endl;
continue;
}
else
{
cout<<"No positions"<<endl;
continue;
}
}
if(pos[1]>=80)
{
if(pos[2]<300)
{
cout<<"No positions"<<endl;
continue;
}
if(pos[2]>=300 && pos[2]<500)
{
cout<<"Defense"<<endl;
continue;
}
if(pos[2]>=500)
{
cout<<"Mid-field Defense"<<endl;
continue;
}
}
}
if(pos[0]>30 && pos[0]<=36)
{
if(pos[1]>=80 && pos[2]>=300)
{
cout<<"Defense"<<endl;
continue;
}
else
{
cout<<"No positions"<<endl;
continue;
}
}
if(pos[0]>36)
{
cout<<"No positions"<<endl;
continue;
}
}
}
在此if
语句if(pos[0]>26 && pos[0]<=30)
中,您必须在pos[1] < 70
时添加一个条件才能打印"无位置"。程序失败的一个示例测试是30 64 377
。
现在,尽管您(几乎)用这种方式解决了问题,但我必须说您的代码写得很糟糕。我的意思是,你有太多的条件,角落大小写挂起和一些变量的糟糕名称。你可以看到john的答案,找到一种更好的方法来实现它。然而,他的解决方案目前并不完美,因此以下是他删除了所有错误的代码:
int correct(int age, int weight, int strength) {
if (age == 0 && weight == 0 && strength == 0)
return 0;
bool no_positions = true;
if (age <= 30 && weight >= 70 && strength >= 500)
{
cout << "Mid-field";
no_positions = false;
}
if (age <= 26 && weight >= 60 && strength >= 200)
{
if (!no_positions) cout << " ";
cout << "Forward";
no_positions = false;
}
if (age <= 36 && weight >= 80 && strength >= 300)
{
if (!no_positions) cout << " ";
cout << "Defense";
no_positions = false;
}
if (no_positions)
cout << "No positions";
cout << endl;
}
请仔细研究此解决方案,找出您遗漏的所有良好做法。
您的逻辑是错误的(而且过于复杂)。这是简单的方法。
while (1)
{
cin >> age >> weight >> strength;
if (age == 0 && weight == 0 && strength == 0)
return 0;
bool no_positions = true;
if (age <= 30 && weight >= 70 && strength >= 500)
{
cout << "Mid-field ";
no_positions = false;
}
if (age <= 26 && weight >= 60 && strength >= 200)
{
cout << "Forward ";
no_positions = false;
}
if (age <= 36 && weight >= 80 && strength >= 300)
{
cout << "Defense ";
no_positions = false;
}
if (no_positions)
cout << "No positions";
cout << endl;
}
编写代码时,请尝试结构,使代码尽可能接近问题的描述。你对所有不同的if
和continue
语句都太过努力了。
相关文章:
- 使用strcpy将char数组的元素复制到另一个数组
- 有符号的int和int-有没有一种方法可以在C++中区分它们
- 有一个打印语句的函数是一种糟糕的编程实践吗
- 在 c++ 中拥有一组结构的正确方法是什么?
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 如何将三维尺寸不固定的三维阵列展平为一维阵列
- nlohmann-json将一个数组插入到另一个数组中
- 有没有一种方法可以在编译时获得作用域类名
- 为什么make_tie不是一件事
- 对于C++中使用智能指针的指针算术限制,有没有一种变通方法
- c++中O(n^(1/3))中一个数的除数的有效计数
- 一种在C++中读取TXT配置文件的简单方法
- 有没有一种方法可以测量c++程序的运行时内存使用情况
- 有没有一种方法可以使用placement new将堆叠对象分配给分配的内存
- 在调用接收数组的方法时,模板化数组大小是不是一种糟糕的做法
- 有没有一种方法可以通过"typedef"为重新定义的基本类型定义特征和强制转换运算符
- 为什么我必须在C++中添加一个赋值符号来声明一个数组
- 我是否需要在下一次转移时将所有权*转移回转移队列
- 有没有一种"cleaner"的方法可以在指向基的指针向量中找到派生类的第一个实例?
- 组建一支足球队