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

标签 c++ arrays

#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/

相关文章:

c++ - 为什么数组没有形成?

c++ - 如何实现类似 "single instance"的设计?

代理服务器的 C# 性能(与 C++ 相比)

c++ - 无法在 32 位服务中使用 RegLoadKey 加载 64 位 key

c++ - 是否有可以与 GCC 4.9.x 一起使用的 GSL 实现?

java - 获取数组组件的类

C++ 使用构造函数参数初始化成员数组

Java将字符串与int数组关联起来

c - 如何让程序停止读取代码?

c++ - 这个类是否满足分配器要求?