问题描述
涛涛最近要负责图书馆的管理工作,需要记录下每天读者的到访情况。每位读者有一个编号,每条记录用读者的编号来表示。给出读者的来访记录,请问每一条记录中的读者是第几次出现。
输入格式
输入的第一行包含一个整数n,表示涛涛的记录条数。
第二行包含n个整数,依次表示涛涛的记录中每位读者的编号。
输出格式
输出一行,包含n个整数,由空格分隔,依次表示每条记录中的读者编号是第几次出现。
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO 自动生成的方法存根
Scanner in=new Scanner(System.in);
int n=in.nextInt();
ArrayList<Short> list=new ArrayList<Short>();//保存原数
ArrayList<Short> count=new ArrayList<Short>();//计数
StringBuilder str=new StringBuilder();
for(int i=0;i<n;i++) {
short temp=in.nextShort();
if(list.contains(temp)) {//计数加一
int index=list.indexOf(temp);
short des=(short)(count.get(index)+1);
count.set(index,des);
str.append(des+" ");
}else {//添加初始计数值1
list.add(temp);
count.add((short)1);
str.append(1+" ");
}
}
System.out.print(str.deleteCharAt(str.length()-1));
in.close();
}
}