CCP-CSP认证考试 201412-1 门禁系统 c/c++题解

题目描述

试题编号: 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的正整数。

题解:

方法1:
我的思路:
数组a用来记录n个数的值
数组cnt用来记录每个数出现的次数(下标为数的具体大小)
数组tmp用来记录每个数出现的次数+1(作用看后面)
比如一个数1出现了3次,那么cnt[1] = 3 tmp[1] = 4
那么循环遍历到这个的时候,
第一次遇到输出tmp[1] - cnt[1] = 1 然后cnt[1]–等于2
第二次遇到输出tmp[1] - cnt[1] = 2 然后cnt[1]–等于1
第三次遇到输出tmp[1] - cnt[1] = 3
就正好是按照1,2,3的顺序输出的。
方法2:
直接用一个map来记录下每个数字出现的次数即可,如果之前没有出现过,输出1,如果出现过,输出出现的次数+1即可。

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cstring>
#include <string>
#include <algorithm>
#include <vector>
#include <deque>
#include <list>
#include <utility>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <bitset>
#include <iterator>
using namespace std;

typedef long long ll;
const int inf = 0x3f3f3f3f;
const ll  INF = 0x3f3f3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double E = exp(1.0);
const int MOD = 1e9+7;
const int MAX = 1e3+5;
int n;
/* 方法1
int number[MAX];
int cnt[MAX];
int tmp[MAX]; */

/* 方法2 */
map <int,int> mp;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    /* 方法1
    while(cin >> n)
    {
        for(int i = 0; i < n; i++)
        {
            cin >> number[i];
            cnt[number[i]]++;
            tmp[number[i]] = cnt[number[i]] + 1;
        }
        for(int i = 0; i < n; i++)
        {
            cout << tmp[number[i]] - (cnt[number[i]]) << " ";
            cnt[number[i]]--;
        }
        cout << endl;
    }*/

    /* 方法2 */
    cin >> n;
    while(n--)
    {
        int x;
        cin >> x;
        map<int,int>::iterator it = mp.find(x);
        if(it == mp.end())
        {
            cout << 1 << " ";
            mp[x] = 1;
        }
        else
        {
            cout << mp[x]+1 << " ";
            mp[x]++;
        }
    }


    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值