gpt4 book ai didi

c++ - 两个数组,整数和字符串,如何显示带有相应整数的字符串?

转载 作者:行者123 更新时间:2023-12-02 10:21:34 29 4
gpt4 key购买 nike

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
string iceCream[5] = {"vanilla", "butter pecan", "superman", "chocolate fudge", "strawberry"};
int iceCreamscoops[5];
int totalScoops = 0;

cout << "enter the total amount of scoops for each icecream flavor\n";
for (int i = 0; i < 5; ++i)
{
cin >> iceCreamscoops[i];
cout << "\n";
}
for (int i = 0; i < 5; ++i)
{
cout << iceCream[i] << " = " << iceCreamscoops[i] << " have been sold"
<< "\n";
totalScoops = totalScoops + iceCreamscoops[i];
}
cout << totalScoops << " total icecream scoops sold";
for (int i = 0; i < 5; ++i)
{
// Change < to > if you want to find the smallest element
if (iceCreamscoops[0] < iceCreamscoops[i])
iceCreamscoops[0] = iceCreamscoops[i];
}
cout << "\nMost popular flavor/flavors and scoops sold = " << iceCreamscoops[0];
for (int i = 0; i < 5; ++i)
{
if (iceCreamscoops[0] > iceCreamscoops[i])
iceCreamscoops[0] = iceCreamscoops[i];
}

cout << "\nLeast popular flavor/flavors and scoops sold = " << iceCreamscoops[0];
}

在上面的代码中,我需要显示与 flavor 相比卖得最多和最少的冰淇淋。如何在输入值旁边显示字符串?我尝试通过获取发现的值并将其与字符串数组进行比较来比较代码。

最佳答案

(您不需要在执行操作时包含cmath。)

跟踪最大和最小数量的瓢以及每个瓢的索引可能会有所帮助,而不是覆盖iceCreamScoops[0]。例如,我们可以创建变量mostScoops + indexOfMostScoopsleastScoops + indexOfLeastScoops,如下所示:

#include <iostream>
using namespace std;

int main()
{
string iceCream[5] = {"vanilla", "butter pecan", "superman", "chocolate fudge", "strawberry "};
int iceCreamScoops[5];
int totalScoops = 0;
int mostScoops = -1, leastScoops = 1E6;
int indexOfMostScoops = -1, indexOfLeastScoops = -1;

cout << "Enter the total amount of scoops for each icecream flavor.\n";
for (int i = 0; i < 5; ++i)
{
cout << iceCream[i] << ": ";
cin >> iceCreamScoops[i];

totalScoops += iceCreamScoops[i];

if (iceCreamScoops[i] > mostScoops)
{
mostScoops = iceCreamScoops[i];
indexOfMostScoops = i;
}

if (iceCreamScoops[i] < leastScoops)
{
leastScoops = iceCreamScoops[i];
indexOfLeastScoops = i;
}
}

cout << "\nThe total number of scoops is " << totalScoops << ".\n";
cout << "The most popular flavor is " << iceCream[indexOfMostScoops] << " with " << mostScoops << " scoops.\n";
cout << "The least popular flavor is " << iceCream[indexOfLeastScoops] << " with " << leastScoops << " scoops.\n";
}

(当然,您可能希望添加检查以确保用户仅输入非负整数作为瓢的数量。)

编辑:

如果您想用特定数量的勺子(例如最多的勺子)来识别所有口味,则甚至无需跟踪索引。例如,您可以执行以下操作:

cout << "\nThe greatest number of scoops is " << mostScoops << ".\n"
<< "The following flavors had this number of scoops:\n";

for (int i = 0; i < 5; i++)
{
if (iceCreamScoops[i] == mostScoops)
{
cout << " " << iceCream[i] << endl;
}
}

查找最大值的基本想法是从一个小于任何期望值的值开始(例如 -1 scoops),然后在每次遇到大于当前最大值的值时更新此最大值。

同样,要找到一个最小值,我们可以从一个大于任何期望值的值开始(例如 1E6 scoops),然后在遇到一个较小的值时更新该最小值。

关于c++ - 两个数组,整数和字符串,如何显示带有相应整数的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59914932/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com