【问题标题】:Pancake Glutton - the one one who ate the leastPancake Glutton - 吃得最少的人
【发布时间】:2018-01-08 16:45:47
【问题描述】:

我正在参加 cplusplus.com 初学者练习,以使用我在 C++ 中学习的内容。我想出了Pancake Glutton 练习。


煎饼饕餮

要求: 变量、数据类型和数值运算符 基本输入/输出 逻辑(if 语句、switch 语句) 循环(for、while、do-while) 数组

编写一个程序,要求用户输入 10 个人(第 1 个人、第 2 个人、...、第 10 个人)早餐吃的煎饼数量 输入数据后,程序必须分析数据并输出哪个人早餐吃的煎饼最多。

★ 修改程序,让它也输出早餐吃的煎饼最少的人。

★★★★ 修改程序,按照10个人吃的煎饼数量的顺序输出一个列表。


我的程序解决了这个问题,但我制作的人数是由用户插入的。我的问题是,当程序寻找吃最少份煎饼的人时,我的程序崩溃了。

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int people,i,maxi=0,cakes, mini=0;
string name,glutton,nibbler;

int main()
{
    //Ask user the amount of people that have eaten pancakes
    cout<<"How many people have eaten pancakes? \n";
    cin>>people;

    //Assign the names of each person that ate pancakes
    string names[people];
    for(i=0;i<people;i++){
        cout<<"Name? ";
        cin>>name;
        names[i]=name;
    }

    cout<<endl;

    //Ask how many pancakes each person has eaten
    int nums[people];
    for(i=0;i<people;i++){
        cout<<"How many pancakes did "<<names[i]<<" eat?"<<endl;
        cin>>cakes;
        nums[i]=cakes;
    }

    cout<<endl;

    //Compare to take the person that ate the most
    for(i=0;i<people;i++){
        if(nums[i]>maxi){
            maxi=nums[i];
            glutton=names[i];
        }
    }

    mini=maxi;
    /*I assigned the value of max to mini to make starting
    *point of comparison to look for the one that ate the
    *least
    */

    //Compare for the person that ate the least
    /**This is what makes my program explode, and I don't know why*/
    for(i=0;i<people;i--){
        if(nums[i]<mini){
            mini=nums[i];
            nibbler=names[i];
        }
    }

    cout<<glutton<<" ate the most"<<endl;
    cout<<nibbler<<" ate the least"<<endl;

    return 0;
}

【问题讨论】:

  • int people...string names[people]; -- int nums[people]; -- 这不是有效的 C++。在 C++ 中,数组是使用编译时常量声明的,以表示数组中的条目数,而不是像 int people 这样的变量。变量数组是在 C++ 中使用 std::vector 完成的。
  • for(i=0;i&lt;people;i--) 这永远不会终止。
  • 使用调试器单步调试代码,看看哪里出错了。另请参阅 Eric Lippert 的 How to debug small programs
  • @MikeHarris 你考虑过整数下溢吗?您的陈述将不适用于整数下溢导致最高可能值的那些(可能广泛传播的)环境。
  • @Yunnosch 你是对的,当然。当整数换行时,循环将终止。更大的问题是,在第一次迭代之后,i 的值将是 -1,这将导致 nums[i] 取消引用出现问题。程序可能会在整数溢出之前崩溃。

标签: c++


【解决方案1】:

基本上我发现我可以使用数组中的位置来使代码更短、更容易理解并且明显更快。我知道这种程序记忆无关紧要,但我应该从一开始就养成这个习惯。

#include <iostream>
#include <string>

using namespace std;

int people,i,glutton=0,cakes,nibbler=0;
string name;

int main()
{
    //Ask user the amount of people that have eaten pancakes
    cout<<"How many people have eaten pancakes? \n";
    cin>>people;

    //Assign the names of each person that ate pancakes
    string names[people];
    for(i=0;i<people;i++){
        cout<<"Name? ";
        cin>>name;
        names[i]=name;
    }

    cout<<endl;

    //Ask how many pancakes each person has eaten
    int nums[people];
    for(i=0;i<people;i++){
        cout<<"How many pancakes did "<<names[i]<<" eat?"<<endl;
        cin>>cakes;
        nums[i]=cakes;
    }

    cout<<endl;

    //Compare to take the person that ate the most and the least

    for(i=1;i<people;i++){
        if(nums[i]>nums[glutton]){
            glutton=i;
        }
        if(nums[i]<nums[nibbler]){
            nibbler=i;
        }
    }

    cout<<names[glutton]<<" ate the most"<<endl;
    cout<<names[nibbler]<<" ate the least"<<endl;

    return 0;
}

【讨论】:

  • 标准库中有工具可以在容器中find the minimal and maximal values。使用它可以避免编写这种原始的低级代码。在组合你自己的方法之前,一定要寻找这样的方法。
  • 投反对票,因为这不是 C++(请参阅我上面的评论)。其他人会在使用 Visual Studio 或符合 ANSI 的编译器时阅读这篇文章,然后尝试运行它,然后会遇到编译器错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-30
  • 2022-08-15
相关资源
最近更新 更多