2015-03-31 114 views
1

我在C++ 现在我被困在一个锻炼我,我不知道如何解决它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; 
} 

的问题是,我不知道如何排序和扭转煎饼分配给正确的人 请帮助和解释

对不起,我的英语

+1

你问过你的老师的帮助吗? – 2015-03-31 13:13:39

+0

您可以使用地图来存储一个值(人数)的值(煎饼数量)。 http://www.cplusplus.com/reference/map/map/ – 2015-03-31 13:15:25

+0

这不是来自学校,我正在学习C++,因为我真的很喜欢编程和这个练习是从网站 – 2015-03-31 13:15:34

回答

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; 
} 
+0

谢谢:)容易理解和工作 – 2015-03-31 13:55:54

+1

使用数量的煎饼作为关键只适用于每个人吃不同数量的煎饼。 – 2015-03-31 13:55:58

+0

取决于你的意思是作品。代码编译并做了一些明智的事情,因为multimap可以处理相同的密钥,但是您得到的顺序本质上是未定义的,因为没有对相等密钥的排序做出保证。如果你不关心订单,那么最后报告的人就没事了。 – Theolodus 2015-03-31 14:02:05

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; 
} 

例如,如果进入的煎饼以下序列

则输出看起来像

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. 
+0

纠正我,如果我错了,但我认为你在那里使用了一些C++ 11(自动关键字和lambda函数?)。这很好(并且C++ 11可能是提问者应该学习的tbh),但是他没有说他使用的是C++ 11。 – Theolodus 2015-03-31 14:14:21

+0

@Theolodus我认为如果他在代码方面会有一些麻烦,他总是可以自己给帖子写评论,不是吗? – 2015-03-31 14:21:28

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++函数来解决这个问题?