首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C++:在数组中查找最大值的问题

C++:在数组中查找最大值的问题
EN

Stack Overflow用户
提问于 2015-11-09 18:09:23
回答 4查看 119关注 0票数 0

感谢你们每一位提前给予的帮助。

问题一:

我今天正在写一个程序,它应该显示用户将输入的10个值中的最高值。然而,每当我测试程序和输入值时,它似乎显示了不止一个“最大值”。我想这可能与我过度使用cout和cin函数有关。程序似乎会沿着用户输入的值列表向下移动,并选择列表中最高的值。

例如:

代码语言:javascript
复制
2, 4, 5, 1, 7, 8, 3, 2, 9

它将选择5、8和9作为最高值。

问题二:

我想知道是否有一种方法可以简化我在下面编写的代码(参考cout和cin函数)。例如,制作一个while函数,并具有如下函数:

代码语言:javascript
复制
 while (something)
      cout << "Please input the number of pancakes person " << x << "has ate.\n"
      cin >> something // Either x or a pancakes function. I tried both.
      count++ // Sorry, I would just assume that the count function would be used in this scenario.

问题3:

关于我的程序中的最大值输出,会不会是我把数组搞乱了?

代码:

代码语言:javascript
复制
#include <iostream>

using namespace std;

int main()
{
    int pancakes [10];
    int x;
    int largestValue = 0;

    cout << "Please input the number of pancakes person 1 has ate.\n";
    cin >> pancakes[0];
    cout << "Please input the number of pancakes person 2 has ate.\n";
    cin >> pancakes[1];
    cout << "Please input the number of pancakes person 3 has ate.\n";
    cin >> pancakes[2];
    cout << "Please input the number of pancakes person 4 has ate.\n";
    cin >> pancakes[3];
    cout << "Please input the number of pancakes person 5 has ate.\n";
    cin >> pancakes[4];
    cout << "Please input the number of pancakes person 6 has ate.\n";
    cin >> pancakes[5];
    cout << "Please input the number of pancakes person 7 has ate.\n";
    cin >> pancakes[6];
    cout << "Please input the number of pancakes person 8 has ate.\n";
    cin >> pancakes[7];
    cout << "Please input the number of pancakes person 9 has ate.\n";
    cin >> pancakes[8];
    cout << "Please input the number of pancakes person 10 has ate.\n";
    cin >> pancakes[9];

    for (int x = 0; x < 10; x++)
        if (pancakes[x] > largestValue)
    {
            largestValue = pancakes[x];
        cout << "The person who ate the most pancakes ate: " << largestValue << " pancakes.\n";
    }

    return 0;

}
EN

回答 4

Stack Overflow用户

发布于 2015-11-09 18:15:33

问题是,每次在这段代码中选择一个新的max-value时都会打印出来:

代码语言:javascript
复制
for (int x = 0; x < 10; x++) {
    if (pancakes[x] > largestValue) {
        largestValue = pancakes[x];
    }
    cout << "The person who ate the most pancakes ate: " << largestValue << " pancakes.\n";
}

相反,它应该是:

代码语言:javascript
复制
for (int x = 0; x < 10; x++) {
    if (pancakes[x] > largestValue) {
        largestValue = pancakes[x];
    }
}
cout << "The person who ate the most pancakes ate: " << largestValue << " pancakes.\n";

顺便说一下:我想你要么用“吃”(不用“有”),要么用“吃过了”。

关于没有硬编码人员数量的另一个问题:首先,我将使用std::vector而不是数组。然后你会问,有多少人。现在,您可以创建一个具有如此多次迭代的for-循环。在每次迭代中,用户都会被问到person X吃了多少煎饼,然后使用std::vector::push_back将这个数字附加到向量的末尾。最后,你,你只需要像你已经做的那样选择最大的数字。只要让for循环的上限为< vector.size()即可。

票数 3
EN

Stack Overflow用户

发布于 2015-11-09 18:17:06

代码语言:javascript
复制
for (int x = 0; x < 10; x++)
    if (pancakes[x] > largestValue)
{
        largestValue = pancakes[x];
    cout << "The person who ate the most pancakes ate: " << largestValue << " pancakes.\n";
}

一旦找到比以前看到的更大的结果,您就会立即显示结果。您应该等到检查完所有的值。

代码语言:javascript
复制
for (int x = 0; x < 10; x++)
    if (pancakes[x] > largestValue)
    {
        largestValue = pancakes[x];
    }

cout << "The person who ate the most pancakes ate: " << largestValue << " pancakes.\n";
票数 1
EN

Stack Overflow用户

发布于 2015-11-09 18:16:09

cout语句应该在循环之外。每次输入if语句时都会打印当前的cout (每次找到新的largestValue时)。

代码语言:javascript
复制
for (int x = 0; x < 10; x++) {
    if (pancakes[x] > largestValue)
    {
        largestValue = pancakes[x];
    }
}

cout << "The person who ate the most pancakes ate: " << largestValue << " pancakes.\n";
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33606721

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档