Access Control System java Solution Code for CCF Test Questions

  • 2021-06-28 09:14:35
  • OfStack

Problem Description

Recently, Tao Tao is responsible for the management of the library and needs to record the visits of readers every day.Each reader has a number, and each record is represented by the reader's number.Given the visitor's record, ask how many times the reader appears in each record.

Input Format

The first line of input contains an integer n, which represents the number of records of Tao.
The second line contains n integers, which in turn represent the number of each reader in Tao's record.

Output Format

Output 1 line containing n integers, separated by spaces, indicating the first occurrence of a reader number in each record.

sample input

5
1 2 1 1 3

sample output

1 1 2 3 1

Measure use case size and conventions

1 < n < 1,000, and the reader's number is a positive integer not exceeding n.

Problem solving code (java):

Method 1:


import java.util.Scanner;
 
public class Main {
 
 public static void main(String[] args) {
 Scanner scanner=new Scanner(System.in);
 int N=scanner.nextInt();
 int[] arr=new int[N];
 int[] arr1=new int[N];
 arr1[0]=1;
 for(int i=0;i<N;i++){
 arr[i]=scanner.nextInt();
 }             
 for(int i=1;i<N;i++){
 int count=1;
 for(int j=i-1;j>=0;j--){
 if((arr[j])!=(arr[i])){
 arr1[i]=count;
 }else{
 count++;     
 arr1[i]=count;
 }      
 } 
 }         
 for(int i=0;i<N;i++){
 System.out.print(arr1[i]+" ");
 }   
 
 
 }
 
}

Method 2:


import java.util.Scanner;
 
public class Main {
 
 public static void main(String[] args) {
 Scanner scanner=new Scanner(System.in);
 int n=scanner.nextInt();
 int[]arr=new int[n];
 for(int i=0;i<arr.length;i++){
 arr[i]=scanner.nextInt();
 }
 for(int i=0;i<arr.length;i++){
 int count=1;
 for(int j=i-1;j>=0;j--){
 if(arr[j]==arr[i]){
 count++; 
 } 
 }
 System.out.print(count+" ");
 }
 
 }
}

Related articles: