试题编号: 201412-1
试题名称: 门禁系统
时间限制: 1.0s
内存限制: 256.0MB
问题描述:
问题描述
涛涛最近要负责图书馆的管理工作,需要记录下每天读者的到访情况。每位读者有一个编号,每条记录用读者的编号来表示。给出读者的来访记录,请问每一条记录中的读者是第几次出现。
输入格式
输入的第一行包含一个整数n,表示涛涛的记录条数。
第二行包含n个整数,依次表示涛涛的记录中每位读者的编号。
输出格式
输出一行,包含n个整数,由空格分隔,依次表示每条记录中的读者编号是第几次出现。
样例输入
5
1 2 1 1 3
样例输出
1 1 2 3 1
评测用例规模与约定
1≤n≤1,000,读者的编号为不超过n的正整数。
解法一 map
key 中存放这个数, value 存放对应的次数,每次输入数都将查找一遍map的key,如果存在,给对应的value 值+1,并将该次数存起来,最后输出。
代码:
#include <iostream>
#include <cstring>
#include <cmath>
#include <map>
#define N 1000
int a[N+1];
int b[N+1];
using namespace std;
int number;
int main()
{
int n,num,i;
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
int num2=0;
int temp=0;
cin>>n;
map<int,int> m;
int v =0;
for(int i=0;i<n;i++){
cin>>v;
m[v]++;
for(map<int,int>::iterator it=m.begin();it!=m.end();it++){
if(it->first==v){
b[num2++]=it->second;
}
}
}
for(int i=0;i<num2;i++){
cout<<b[i]<<" ";
}
return 0;
}