201412-1 门禁系统

问题描述

涛涛最近要负责图书馆的管理工作,需要记录下每天读者的到访情况。每位读者有一个编号,每条记录用读者的编号来表示。给出读者的来访记录,请问每一条记录中的读者是第几次出现。


输入格式
输入的第一行包含一个整数n,表示涛涛的记录条数。
第二行包含n个整数,依次表示涛涛的记录中每位读者的编号。

输出格式
输出一行,包含n个整数,由空格分隔,依次表示每条记录中的读者编号是第几次出现。

样例

输入: 5
            1 2 1 1 3
输出: 1 1 2 3 1

解题思路

用 map 存计数就可以了

import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String in = sc.nextLine();
		in = sc.nextLine();
		String[] temp = in.split(" ");
		List<Integer> student = new ArrayList<>();
		int i;
		for(i = 0; i < temp.length; i++)
			student.add(Integer.parseInt(temp[i]));
		Map<Integer, Integer> count = new HashMap<>();
		List<Integer> answer = new ArrayList<>();
		for(i = 0; i < temp.length; i++) {
			if(count.containsKey(student.get(i)))
				count.put(student.get(i), count.get(student.get(i)) + 1);
			else
				count.put(student.get(i), 1);
			answer.add(count.get(student.get(i)));
		}
		System.out.print(answer.get(0));
		for(i = 1; i < answer.size(); i++)
			System.out.print(" " + answer.get(i));
		System.out.println();
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值