输入在第1行给出不超过105的正整数N,即学生总人数。随后1行给出N名学生的百分制整数成绩,中间以空格分隔。最后1行给出要查询的分
数个数K(不超过N的正整数),随后是K个分数,中间以空格分隔。
在一行中按查询顺序给出得分等于指定分数的学生人数,中间以空格分隔,但行末不得有多余空格。
10 60 75 90 55 75 99 82 90 75 50 3 75 90 88
3 2 0
import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner in=new Scanner (System.in); int n=in.nextInt(); int []num=new int[101]; for(int i=0;i<n;i++) { num[in.nextInt()]++; } int m=in.nextInt(); for(int i=0;i<m;i++) { if(i==m-1) { System.out.println(num[in.nextInt()]); break; } System.out.print(num[in.nextInt()]+" "); } } }
//本代码思路:使用map装入数据,key装成绩,value装人数,然后按查询输出结果 import java.util.Scanner; import java.util.Map; import java.util.TreeMap; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNext()){ int n = sc.nextInt(); Map<Integer,Integer> map = new TreeMap<>(); for(int i=0;i<n;i++){ int k = sc.nextInt(); if(map.containsKey(k)){ map.put(k,map.get(k)+1); }else{ map.put(k,1); } } int num = sc.nextInt(); for(int i=0;i<num;i++){ int key = sc.nextInt(); if(map.containsKey(key)){ System.out.print(map.get(key)); }else{ System.out.print(0); } if(i != num-1){ System.out.print(" "); } } } } }
import java.util.HashMap;import java.util.Scanner;public class Pat_1028 { private int N; private int[] numbers; //保存所有分数 private int n; //记录需要查看的分数个数 private int[] targets; //保存查看的分数 private HashMap<Integer,Integer> map; //键-值:分数-次数 //构造器初始化所有变量 public Pat_1028() { Scanner sc = new Scanner(System.in); N = sc.nextInt(); numbers = new int[N]; map = new HashMap<>(); for(int i=0;i<N;i++) { numbers[i] = sc.nextInt(); } n = sc.nextInt(); targets = new int[n]; for(int i=0;i<n;i++) { targets[i] = sc.nextInt(); } sc.close(); } //将所有的分数保存在Map中,键为分数,值为出现次数 public void getMap() { for(int i=0;i<N;i++) { if(map.containsKey(numbers[i])) { map.put(numbers[i], map.get(numbers[i])+1); }else { map.put(numbers[i], 1); } } } //查看分数并输出 public void printResult() { for(int i=0;i<n;i++) { if(map.containsKey(targets[i])) { System.out.print(map.get(targets[i])); }else { System.out.print("0"); } if(i<n-1) System.out.print(" "); } } public static void main(String[] args) { Pat_1028 main = new Pat_1028(); main.getMap(); main.printResult(); } }
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int count =0; int n = sc.nextInt(); int[] a = new int[n]; for(int i =0 ;i <n;i++) a[i] = sc.nextInt(); int k = sc.nextInt(); for(int j = 1;j <= k;j++){ count =0; int b = sc.nextInt(); for(int j1 = 0;j1 < n;j1++){ if(a[j1] == b ) count++; } if(count >= 0){ if(j < k) System.out.print(count + " "); else System.out.println(count); } } } }
}