public static void simpleRank() { int count = 5; int[] score = { 80, 70, 75, 100, 75 }; int[] juni = { 0, 0, 0, 0, 0 }; for (int i = 0; i < count; i++) { juni[i] = 1; for (int j = 0; j < count; j++) { if (score[j] > score[i]) juni[i]++; } } System.out.println("得分\t排行\n"); for (int i = 0; i < count; i++) System.out.println("得分:" + score[i] + "排行:" + juni[i]); } public static void optimizeRank() throws IOException { final int MAX = 100; final int MIN = 0; int[] score = new int[MAX + 1]; int[] juni = new int[MAX + 2]; BufferedReader reader = new BufferedReader(new InputStreamReader( System.in)); int count = 0; do { System.out.print("输入分数,-1结束:"); score[count++] = Integer.parseInt(reader.readLine()); } while ((score[count - 1] != -1)); count--; for (int i = 0; i < count; i++) juni[score[i]]++; juni[MAX + 1] = 1; for (int i = MAX; i >= MIN; i--) juni[i] = juni[i] + juni[i + 1]; System.out.println("得分\t排行"); for (int i = 0; i < count; i++) { System.out.println(score[i] + "\t" + juni[score[i] + 1]); } }
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/15760276/viewspace-663873/
,如需转载,请注明出处,否则将追究法律责任。