本題要求讀入N名學生的成績,將獲得某一給定分數的學生人數輸出。
輸入格式:
輸入在第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
java做最後一個點會超時
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int[] arrScore = new int[101];
Scanner in = new Scanner(System.in);
int n = in.nextInt();
for(int i = 1;i <= n; i++){
int x = in.nextInt();
arrScore[x]++;
}
int find = in.nextInt();
while(find != 0){
int temp = in.nextInt();
if(find != 1){
System.out.print(arrScore[temp] + " ");
}else{
System.out.print(arrScore[temp]);
}
find--;
}
}
}
之前未剪枝:
如圖