当我需要知道最大值和谁达到最大值时使用什么容器?

What container to use when I need to know both the max value and who has achieved it?

我正在尝试完成网站上的初学者练习。

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

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

我不确定如何让程序调出吃煎饼次数最多的人?当然,这需要使用键和值来完成,但要求说明 'arrays' 而不是 'maps'?

下面是我想出的代码,但这只输出了吃煎饼的最大数量,所以不是真正回答问题!

非常感谢您的帮助!

* 我只用了 5 个人来加快这个过程,然后我就知道该怎么做了 *

#include <iostream>
using namespace std;

int main()
{
    cout << "how many pancakes did you eat for breakfast?" << endl;

    int person1, person2, person3, person4, person5;
    cout << "Person 1: ";
    cin >> person1;

    cout << "Person 2: ";
    cin >> person2;

    cout << "Person 3: ";
    cin >> person3;

    cout << "Person 4: ";
    cin >> person4;

    cout << "Person 5: ";
    cin >> person5;

    int array[5] = {person1, person2, person3, person4, person5};
    int temp = 0;

    for (int i = 0; i<5; i++)
    {
        if (array[i] > temp)
        {
        temp = array[i];
        }
    }
    cout << "The most pancakes eaten was " << temp << "by " <<  endl;

}

Surely this would need to be done with a key and value

这不是唯一的方法。另一种方法是使用没有键的索引集合,并假设位置 k 对应于可以单独从位置计算的键 k。例如,如果您有一个包含十个项目的数组,对应于编号为 1 到 10 的十个人,那么可以将编号 k 的人的数据存储在数组中的 k-1 位置。在这种情况下不需要密钥。

这个冗长的解释意味着如果除了最好的 tmp 之外你还存储最好的 i,你将在循环后得到你的答案:

int temp = 0;
int res = -1;
for (int i = 0; i<5; i++) {
    if (array[i] > temp) {
        temp = array[i];
        res = i;
    }
}
cout << "The most pancakes eaten was " << temp << "by " <<  (res+1) << endl;

请注意打印的是 res+1,而不是 res。这是因为数组是从零开始的,而计数是从一开始的。

这可以使用一个常见的习惯用法进一步缩短,即使用初始元素作为当前最佳元素,并从 1:

开始迭代
int res = 0;
for (int i = 1 ; i<5 ; i++) {
    if (array[i] > array[res]) {
        res = i;
    }
}
cout << "The most pancakes eaten was " << array[res] << "by " <<  (res+1) << endl;

如果您在输入时跟踪最大吃煎饼量会怎样?

#include <iostream>

using namespace std;

// To execute C++, please define "int main()"

int main() {
  int numPeople = 5;

  int maxPancakes = -1;
  int maxPerson = -1;

  int currentPancakes = -1; 

  for (int i = 1; i < numPeople; i++) {
    cout << "Person " << i << ": ";
    cin >> currentPancakes;

    if (currentPancakes > max) {
      max = currentPancakes;
      maxPerson = i;
    }
  }

  cout << "Person " << maxPerson << " ate the most pancakes: " << maxPancakes;

  return 0;
}

注意:我的 C++ 很生疏,我还没有测试过这个解决方案。只是一个想法 ;)

用 Map 做这道题就有点矫枉过正了。阵列绰绰有余。您甚至不需要遍历数组来检查谁吃得最多。获得最大值的操作实际上是 O(0) 因为我们可以在您输入值时更新谁吃得最多。

int main(){
    const int NUM_PEOPLE = 10;
    int cakesEaten[10] = {0}; 
    int maxEaten = 0;
    int personId = 0;

    cout << "How many pancakes eaten by:" << endl;
    for(int x=0; x<NUM_PEOPLE; x++){
        cout << "person " << (x+1) << ":";
        cin >> cakesEaten[x];

        if (cakesEaten[x] > maxEaten){
            maxEaten = cakesEaten[x];
            personId = x;
        }
    }
    cout << "The most pancakes was eaten by person " << personID << endl;
}

您根本不需要任何存储空间。

输入数字后,比较它们并存储谁拥有当前最大值及其值

如果您使用第一人称的值作为起始值,则不需要以假值开头,这样输入时可以包含负值。这在这里可能是荒谬的,但总的来说,这是一种更好的做法。

另请注意,如果我们希望 people1 开始,那么从 1 开始更有意义,然后从 0 开始并尝试请记住始终添加 1.

这个也很容易扩展到更多人,改成total_people

int main() {
  const int total_people=5;

  cout << "how many pancakes did you eat for breakfast?" << endl;

  int what;
  cout << "Person 1: ";
  cin >> what;

  int who=1;
  int max_value=what;

  for (int person = 2; person <= total_people; ++person) { 
    cout << "Person " << person << ": ";
    cin >> what;
    if (what > max_value) {
      max_value=what;
      who=i;
    }
  }
  cout << "The most pancakes eaten was " << max_value << "by " << who << endl;
}