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