组建一支足球队

Setting up a Football Team

本文关键字:一支 足球队      更新时间:2023-10-16

你好,我有问题。我的代码适用于所有号码,但我不知道当我发送它时,我得到了错误的答案。这里是问题的链接。任何帮助都可能非常完整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;
}

编写代码时,请尝试结构,使代码尽可能接近问题的描述。你对所有不同的ifcontinue语句都太过努力了。