首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >数组的C++排序

数组的C++排序
EN

Stack Overflow用户
提问于 2013-05-19 18:39:02
回答 3查看 1.5K关注 0票数 0

我在一个c++论坛上找到了一组初学者作业,但现在我完全被困在一个任务中了。任务如下:

编写一个程序,要求用户输入10个不同的人( 1人、2人、.人、10人)早餐吃薄煎饼的数量,一旦输入数据,程序必须分析数据,并输出早餐吃的煎饼最多的人。 ★修改程序,使它还输出谁吃的煎饼数量最少的早餐。 ★★★★对程序进行修改,使其按10人吃薄煎饼的数量顺序输出一个列表。

现在我已经整理好了最初的部分和一颗星位,但是我选择了让它对我自己来说更难一些,而不仅仅是"Person 1,2,3,4等等,而是给字符分配了名字,然后用switch打印出来。下面是我的当前代码,我想要一些建议,我可以在不弄乱数组中的数字顺序的情况下对数组进行排序,因为这会给我的命名代码带来混乱。“

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

代码语言:javascript
复制
#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;
}
EN

回答 3

Stack Overflow用户

发布于 2013-05-19 19:23:40

下面是一个简单的例子。有更好的方法来做到这一点,但这应该给你一个基本的想法的方式之一是这样做。

代码语言:javascript
复制
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++
}
票数 1
EN

Stack Overflow用户

发布于 2013-05-19 23:20:46

沃尔夫冈斯凯勒的回答描述了一个排序算法。您需要在算法中添加一小部分:记住元素是如何交换的。一个容易理解的解决方案是一个额外的数组,它启动排序,并以与score数组完全相同的方式排列:

代码语言:javascript
复制
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中的数据

代码语言:javascript
复制
struct MyComparison
{
    ...
    bool operator()(BreakfastNames name1, BreakfastNames name2)
    {
        ...
        return whatever1 < whatever2;
    }
};

sort(names, names + 10, MyComparison(scores));
票数 1
EN

Stack Overflow用户

发布于 2013-05-19 19:22:25

因此,我已经用以下代码对其中的大部分内容进行了排序:

代码语言:javascript
复制
sort(anArray, anArray + 10);

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

现在的问题是,我如何解决它,以得到的名字旁边的结果。我觉得我应该回去做多维数组,所以我会得到一个设定的值,以及吃薄煎饼的数量,这是一个很好的方法,还是你会推荐其他的吗?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16638132

复制
相关文章

相似问题

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