C++;对数组排序 我在一个C++论坛上发现了一组初学者作业,但现在我完全陷入了一个任务。 任务如下:
编写一个程序,要求用户输入煎饼的数量 由10个不同的人(1人,2人,…)吃早餐。。。, 人员10)一旦输入数据,程序必须分析 数据和输出哪个人早餐吃的煎饼最多 ★ 修改程序,使其也能输出吃了食物的人 早餐吃的煎饼最少 ★★★★ 修改程序,使其按数字顺序输出列表 10个人吃的煎饼 现在我已经将原始位和一星位进行了分类,但是我选择了让自己更难一点,而不仅仅是使用“Person 1、2、3、4等等”,而是为字符指定了名称,并使用C++;对数组排序 我在一个C++论坛上发现了一组初学者作业,但现在我完全陷入了一个任务。 任务如下: ,c++,arrays,sorting,C++,Arrays,Sorting,编写一个程序,要求用户输入煎饼的数量 由10个不同的人(1人,2人,…)吃早餐。。。, 人员10)一旦输入数据,程序必须分析 数据和输出哪个人早餐吃的煎饼最多 ★ 修改程序,使其也能输出吃了食物的人 早餐吃的煎饼最少 ★★★★ 修改程序,使其按数字顺序输出列表 10个人吃的煎饼 现在我已经将原始位和一星位进行了分类,但是我选择了让自己更难一点,而不仅仅是使用“Person 1、2、3、4等等”,而是为字符指定了名称,并使用开关打印出来。 这是我当前的代码,我希望能得到一些建议,告诉我如何对数组进
开关打印出来。
这是我当前的代码,我希望能得到一些建议,告诉我如何对数组进行排序,而不会弄乱数组中数字的顺序,因为我想这会弄乱我的命名代码
这是我的代码,我知道这不是最漂亮的代码,但它现在是功能性的
#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;
}
#包括“stdafx.h”
#包括
使用名称空间std;
int main()
{
int nMostPancakesName;
国际煎饼协会;
枚举早餐名称
{
奈德,//0
雅莉亚,//1
乔恩,//2
罗布,//3
桑萨,//4
卡特琳,//5
麸皮,//6
席恩,//7
霍多尔,//8
鬼//9
};
国际原子能机构[10];
不能乱来[内德];
阿纳雷[雅利安];
库特·阿纳雷[乔恩];
库特·阿纳雷[罗布];
可不可以(SANSA);;
库特·阿纳雷[卡特林];
不能吃面包[麸皮];
库特·阿纳雷[席恩];
库特·阿纳雷[霍多尔];
不能乱来[鬼魂];
int nmaxpankes=0;
对于(int npacakes=0;npacakes<10;npacakes++)
if(anArray[nPancakes]>nMaxPancakes)
{
nmostancakesname=npacakes;
nMaxPancakes=anArray[nPancakes];
}
int NLEASTPACKES=100;
对于(int-npacakes2=0;npacakes2<10;npacakes2++)
if(一列[nPancakes2] cout所以我用以下代码对大部分内容进行了排序:
sort(anArray, anArray + 10);
for(int ooo=0; ooo < 10; ooo++)
{
cout << anArray[ooo] << endl;
}
排序(anArray,anArray+10);
对于(int-ooo=0;ooo<10;ooo++)
{
cout这里有一个简单的例子。有更好的方法可以做到这一点,但这应该能让你对其中一种方法有一个基本的了解
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++
}
int分数[10];
积分检查=0;
//初始化数组“score”
对于(ScoreCheck=0;ScoreCheck<10;ScoreCheck++)
{
score[ScoreCheck]=ScoreCheck;//把每个人放在某个地方
}
分数检查=0;
while(ScoreCheck<9)//检查我们是否找到了最后一个人
{
//检查图表上较低的人是否吃得更多
if(anArray[分数[分数检查]]
沃尔夫冈·斯凯勒(Wolfgang Skyler)的描述描述了一个。您需要在算法中添加一个小的内容:记住元素是如何交换的。一个易于理解的解决方案是添加一个数组,该数组开始排序,并以与分数完全相同的方式进行排列。
数组:
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;
...
即使未实现排序算法,也可以进行调整。要进行调整,请使用的第三个参数,以便对数组名称进行排序,但比较分数中的数据:
struct MyComparison
{
...
bool operator()(BreakfastNames name1, BreakfastNames name2)
{
...
return whatever1 < whatever2;
}
};
sort(names, names + 10, MyComparison(scores));
struct MyComparison
{
...
bool运算符()(BreakfastNames名称1,BreakfastNames名称2)
{
...
返回whatever1
它必须是一个数组吗?你可以使用std::vector和sort()via。你也可以用同样的方法对数组进行排序。@C.Lang,然后你可以使用一个映射,并用同一个程序回答所有问题。请参阅“如何使用std::sort在C++中对数组排序”:@7stud:很好!我读了标题,然后开始键入:(向量部分是因为OP使用数组时每个人都这么建议。有一次我这么做了,我被发现睡不着觉。看我上面的帖子,排序问题解决了。不过我很难用名字打印出来。