首页 > 其他 > 详细

The Department of Redundancy Department

时间:2020-09-26 09:08:13      阅读:45      评论:0      收藏:0      [点我收藏+]

Write a program that will remove all duplicates from a sequence of integers and print the list of uniqueintegers occuring in the input sequence, along with the number of occurences of each.

Input

The input file will contain a sequence of integers (positive, negative, and/or zero). The input file maybe arbitrarily long.

Output

The output for this program will be a sequence of ordered pairs, separated by newlines. The firstelement of the pair must be an integer from the input file. The second element must be the numberof times that that particular integer appeared in the input file. The elements in each pair are to beseparated by space characters. The integers are to appear in the order in which they were containedin the input file.

Sample Input

3 1 2 2 1 3 5 3 3 2

Sample Output

3 4

1 2

2 3

5 1

 

问题简述:统计数的出现次数,按数出现的顺序输出那个数和出现的次数。

程序说明:

  使用map可以简单第进行统计,但是顺序无法保证。

  使用vector来存储数出现的顺序。

 

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 int main()
 4 {
 5     int x;
 6     vector<int> v;
 7     map<int,int> m;
 8     while(cin>>x)
 9     {
10         if(m.find(x)==m.end()){
11             m.insert(make_pair(x,1));
12             v.push_back(x);
13         }
14         else{
15             m[x]++;
16         }
17     }
18     for(int i=0;i<v.size();i++)
19     {
20         cout<<v[i]<<" "<<m[v[i]]<<endl;
21     }
22 }

 

The Department of Redundancy Department

原文:https://www.cnblogs.com/csx-zzh/p/13733696.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!