我是c ++的初学者 现在我被困在练习上,我不知道如何解决它
练习的目标是:写一个程序,要求用户输入由10个不同的人吃早餐的煎饼数量,并制作从最高到最小的列表,如: 人6吃:10个煎饼 人1吃:6煎饼 等...
我有这段代码:
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
int person_num = 0;
int pancakes;
vector<int> pancake_list;
while (true) {
person_num = person_num + 1;
cout << "Please enter the number of pancakes eaten by Person #" << person_num << " ";
cin >> pancakes;
pancake_list.push_back(pancakes);
sort(begin(pancake_list), end(pancake_list));
reverse(begin(pancake_list), end(pancake_list));
if (person_num == 10) {
system("cls");
for (int i = 0; i < pancake_list.size(); i++) {
cout << pancake_list[i] << endl;
}
system("pause");
}
}
return 0;
}
问题在于我不知道如何将分类和反转的煎饼分配给合适的人 请帮忙解释
抱歉我的英文
答案 0 :(得分:0)
我会对c++ map做点什么。如果您知道要对事物进行排序,则可以使用地图键排序来使您的生活更轻松。在这种情况下,使用在地图中作为关键字吃的煎饼数量,以及人数作为值 - 列表已经排序的方式,您可以向后迭代以反向排序顺序打印它。像这样:
#include "stdafx.h"
#include <iostream>
#include <map>
using namespace std;
int main()
{
int person_num = 0;
int pancakes;
multimap<int, int> pancake_map;// Use multimap incase two people ate the same number of pancakes. Note: there are no guarantees about the relative order of elements with the same key.
while (true) {
person_num = person_num + 1;
cout << "Please enter the number of pancakes eaten by Person #" << person_num << " ";
cin >> pancakes;
pancake_map.insert(make_pair(pancakes,person_num));// c++ multimap stores things sorted by key value, so we don't have to do any sorting
if (person_num == 10)
{
system("cls");
// to print the ascending sorted list, iterate through the container forwards
for (multimap<int,int>::iterator it=pancake_map.begin(); it != pancake_map.end(); ++it)
{
cout << "Person number " << it->second << " ate " << it->first << " pancakes." << endl;
}
cout << endl;// little bit of formatting...
// to print the reverse sorted list, iterate backwards
for (multimap<int,int>::reverse_iterator it=pancake_map.rbegin(); it != pancake_map.rend(); ++it)
{
cout << "Person number " << it->second << " ate " << it->first << " pancakes." << endl;
}
system("pause");
break; // I added this to escape the loop after entering 10 people, remove it if you want.
}
}
return 0;
}
答案 1 :(得分:0)
我认为可以使用数组而不是向量,因为元素的数量很少并且是固定的。
其中一个解决方案可以采用以下方式
#include <iostream>
#include <utility>
#include <algorithm>
int main()
{
const size_t N = 10;
std::pair<size_t, size_t> pancake_list[N];
std::cout << "Please enter the number of pancakes eaten by "
<< N << " persons: ";
size_t n = 0;
for ( ; n < N && std::cin >> pancake_list[n].second; n++ )
{
pancake_list[n].first = n + 1;
}
std::sort( pancake_list, pancake_list + n,
[]( auto &a, auto &b ) { return b.second < a.second; } );
for ( size_t i = 0; i < n; i++ )
{
std::cout << "Person #" << pancake_list[i].first
<< " ate " << pancake_list[i].second
<< " pancakes." << std::endl;
}
return 0;
}
例如,如果要输入以下煎饼序列
6 2 8 7 1 4 6 3 5 0
然后输出看起来像
Person #4 ate 7 pancakes.
Person #1 ate 6 pancakes.
Person #7 ate 6 pancakes.
Person #9 ate 5 pancakes.
Person #6 ate 4 pancakes.
Person #8 ate 3 pancakes.
Person #2 ate 2 pancakes.
Person #5 ate 1 pancakes.
Person #10 ate 0 pancakes.
答案 2 :(得分:0)
到目前为止,其他解决方案是使用一对(人#,煎饼)整数。这是一个解决方案。另一个是要意识到你的问题是由于你没有遵守原始订单。对结果进行排序时,您使用新订单替换了原始订单。但如果您对副本进行排序,您仍然可以使用原始订单,并且可以匹配这两个。
以您的代码为基础:
// Ask the use how many pancakes
vector<int> pancake_copy = pancake_list;
sort(begin(pancake_copy), end(pancake_copy));
reverse(begin(pancake_copy), end(pancake_copy));
// You know have the sorted and the unsorted lists.
for (int numPancakes : pancake_copy)
{
std::cout << numPancakes " pancakes were eaten by: ";
for (int person = 0; person != pancake_list.size(); ++person)
{
if (pancakes_list[person] == numPancakes)
{
// This person ate numPancakes
std::cout << i << " ";
}
}
std::cout << std::endl;
}
奖金锻炼:当两个人吃相同数量的煎饼时,现在出了什么问题?您可以使用哪种C ++函数来解决此问题?