0

我正在关注我在 C++ 论坛上找到的一组初学者作业,但我现在完全陷入了一项任务。任务如下:

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

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

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

现在我已经整理出了原始位和一星位,但是我选择让自己更困难一点,而不是仅仅使用“人 1、2、3、4 等等,而是我已经分配给字符命名,然后用 . 打印出来switch。这是我当前的代码,我希望得到一些关于如何在不弄乱数组中数字顺序的情况下对数组进行排序的建议,因为这会弄乱我的命名我猜的代码。

这是我的代码,我知道它不是最漂亮的代码,但它现在可以使用。

#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
    int nMostPancakesName;
    int nLeastPancakesName;
    enum BreakfastNames
    {
        NED, // 0
        ARYA, // 1
        JON, // 2
        ROBB, // 3
        SANSA, // 4
        CATELYN, // 5
        BRAN, // 6
        THEON, // 7
        HODOR, // 8
        GHOST // 9
    };

    int anArray[10];
    cout << "Enter the number of pancakes Ned ate for breakfast: " << endl;
    cin >> anArray[NED];
    cout << "How many did Arya eat?" << endl;
    cin >> anArray[ARYA];
    cout << "And Jon?" << endl;
    cin >> anArray[JON];
    cout << "What about Robb?" << endl;
    cin >> anArray[ROBB];
    cout << "Did Sansa have any?" << endl;
    cin >> anArray[SANSA];
    cout << "Catelyn?" << endl;
    cin >> anArray[CATELYN];
    cout << "Crippleboy aka Bran?" << endl;
    cin >> anArray[BRAN];
    cout << "The traitor didn't get any, did he?" << endl;
    cin >> anArray[THEON];
    cout << "Hodor?" << endl;
    cin >> anArray[HODOR];
    cout << "No pets at the dining table, Ghost." << endl;
    cin >> anArray[GHOST];

    int nMaxPancakes = 0;
    for (int nPancakes = 0; nPancakes < 10; nPancakes++)
        if (anArray[nPancakes] > nMaxPancakes)
        {
            nMostPancakesName = nPancakes;
            nMaxPancakes = anArray[nPancakes];
        }


    int nLeastPancakes = 100;
    for (int nPancakes2 = 0; nPancakes2 < 10; nPancakes2++)
        if (anArray[nPancakes2] < nLeastPancakes)
        {
            nLeastPancakesName = nPancakes2;
            nLeastPancakes = anArray[nPancakes2];
        }

        for (int nStartIndex = 0; nStartIndex < 10; nStartIndex++)
        {
            int nSmallestIndex = nStartIndex;

            for (int nCurrentIndex = nStartIndex + 1; nCurrentIndex < 10; nCurrentIndex++)
            {
                if (anArray[nCurrentIndex] < anArray[nSmallestIndex])
                    nSmallestIndex = nCurrentIndex;
            }


        }


    switch(nMostPancakesName)
    {
    case 0:
        cout << "Ned had " << nMaxPancakes << endl;
        break;
    case 1:
        cout << "Arya had " << nMaxPancakes << endl;
        break;
    case 2:
        cout << "Jon had " << nMaxPancakes << endl;
        break;
    case 3:
        cout << "Robb had " << nMaxPancakes << endl;
        break;
    case 4:
        cout << "Sansa had " << nMaxPancakes << endl;
        break;
    case 5:
        cout << "Catelyn had " << nMaxPancakes << endl;
        break;
    case 6:
        cout << "Bran had " << nMaxPancakes << endl;
        break;
    case 7:
        cout << "Theon had " << nMaxPancakes << endl;
        break;
    case 8:
        cout << "Hodor had " << nMaxPancakes << endl;
        break;
    case 9:
        cout << "Ghost had " << nMaxPancakes << endl;
        break;
    }

    switch(nLeastPancakesName)
    {
    case 0:
        cout << "Ned had " << nLeastPancakes << endl;
        break;
    case 1:
        cout << "Arya had " << nLeastPancakes << endl;
        break;
    case 2:
        cout << "Jon had " << nLeastPancakes << endl;
        break;
    case 3:
        cout << "Robb had " << nLeastPancakes << endl;
        break;
    case 4:
        cout << "Sansa had " << nLeastPancakes << endl;
        break;
    case 5:
        cout << "Catelyn had " << nLeastPancakes << endl;
        break;
    case 6:
        cout << "Bran had " << nLeastPancakes << endl;
        break;
    case 7:
        cout << "Theon had " << nLeastPancakes << endl;
        break;
    case 8:
        cout << "Hodor had " << nLeastPancakes << endl;
        break;
    case 9:
        cout << "Ghost had " << nLeastPancakes << endl;
        break;
    }

    return 0;
}
4

3 回答 3

1

Wolfgang Skyler的回答描述了一种排序算法。你需要对算法做一个小的补充:记住元素是如何交换的。一个易于理解的解决方案是一个额外的数组,它开始排序,并以与数组完全相同的方式score排列:

BreakfastNames names[] = {
    NED, // 0
    ARYA, // 1
    JON, // 2
    ROBB, // 3
    SANSA, // 4
    CATELYN, // 5
    BRAN, // 6
    THEON, // 7
    HODOR, // 8
    GHOST // 9
};

// Code by Wolfgang Skyler goes here
    ...
    // swap person 1 and 2, since 2 ate more that 1
    int tmp = score[ScoreCheck];
    score[ScoreCheck]   = score[ScoreCheck+1];
    score[ScoreCheck+1] = tmp;

    BreakfastNames tmp1 = names[ScoreCheck];
    names[ScoreCheck]   = names[ScoreCheck+1];
    names[ScoreCheck+1] = tmp1;
    ...

即使您没有实现排序算法,也可以调整您的解决方案。为此,请使用的第三个参数,std::sort以便对数组进行排序,names但比较中的数据scores

struct MyComparison
{
    ...
    bool operator()(BreakfastNames name1, BreakfastNames name2)
    {
        ...
        return whatever1 < whatever2;
    }
};

sort(names, names + 10, MyComparison(scores));
于 2013-05-19T23:20:46.507 回答
1

这是一个简单的例子。有更好的方法可以做到这一点,但这应该让您对其中一种方法有一个基本的了解。

int score[10];
int ScoreCheck = 0;

// initalize the array "score"
for (ScoreCheck = 0; ScoreCheck < 10; ScoreCheck++)
{
    score[ScoreCheck] = ScoreCheck; // just put everone somewhere
}

ScoreCheck = 0;
while ( ScoreCheck < 9 ) // check to see if we've reached the last person
{
    // check to see if the person lower on the chart ate more
    if ( anArray[score[ScoreCheck]] < anArray[score[ScoreCheck+1]] )
    {
        // swap person 1 and 2, since 2 ate more that 1
        int tmp = score[ScoreCheck];
        score[ScoreCheck]   = score[ScoreCheck+1];
        score[ScoreCheck+1] = tmp;

        // now go back to the beggining to make
        // sure they are in order from begining to end
        ScoreCheck = 0;
        continue;
    }

    // nope, it's in order so far
    // increment to the next person on the chart
    ScoreCheck++
}
于 2013-05-19T19:23:40.117 回答
0

所以我用以下代码对大部分内容进行了排序:

sort(anArray, anArray + 10);

for(int ooo=0; ooo < 10; ooo++)
{
    cout << anArray[ooo] << endl;
}

现在的问题是我将如何解决它以获取结果旁边的名称。我觉得我应该回去制作多维数组,所以我会得到一个名称的设定值以及吃的煎饼的数量,这是一个很好的方法还是你会推荐别的东西?

于 2013-05-19T19:22:25.643 回答