Loading... 天梯赛是个善良的比赛。善良的命题组希望将题目难度控制在一个范围内,使得每个参赛的学生都有能做出来的题目,并且最厉害的学生也要非常努力才有可能得到高分。 于是命题组首先将编程能力划分成了 $10^6$ 个等级(太疯狂了,这是假的),然后调查了每个参赛学生的编程能力。现在请你写个程序找出所有参赛学生的最小和最大能力值,给命题组作为出题的参考。 ## 输入格式: 输入在第一行中给出一个正整数$N(\leq 2\times 10^4)$,即参赛学生的总数。随后一行给出 $N$ 个不超过 $10^6$ 的正整数,是参赛学生的能力值。 ## 输出格式: 第一行输出所有参赛学生的最小能力值,以及具有这个能力值的学生人数。第二行输出所有参赛学生的最大能力值,以及具有这个能力值的学生人数。同行数字间以 1 个空格分隔,行首尾不得有多余空格。 ## 输入样例: ``` 10 86 75 233 888 666 75 886 888 75 666 ``` ## 输出样例: ``` 75 3 888 2 ``` --- ``` import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int[] arr = new int[N]; int min=1000000,max=0,m = 0,n = 0; for(int i = 0 ; i < arr.length;i++){ arr[i] = sc.nextInt(); if(min>arr[i]) min = arr[i]; if(max<arr[i]) max = arr[i]; } for (int i : arr) { if (min == i) m++; if (max == i) n++; } System.out.print(min+" "+m+"\n"+max+" "+n); } } ``` ``` import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int countMax = 0, countMin = 0; //最大值和最小值的重复次数 int max = 0, min = 1000000; int n = sc.nextInt(); while (n-- > 0) { int num = sc.nextInt(); if (num > max) { max = num; countMax = 1; } else if (num == max) { countMax++; } if (num < min) { min = num; countMin = 1; } else if (num == min) { countMin++; } } System.out.print(min+" "+countMin+"\n"+max+" "+countMax); } } ``` <div class="tip inlineBlock error"> 可以看到,不论用上方的哪种方法,都会使程序超时,所以我们需要对输入方式进行优化 </div> 优化方式可参考: <div class="preview"> <div class="post-inser post box-shadow-wrap-normal"> <a href="https://blog.mol.ink/skills/java_timeout_solution.html" target="_blank" class="post_inser_a no-external-link no-underline-link"> <div class="inner-content" style="margin-left: 10px;"> <p class="inser-title">刷题使用JAVA提交总是超时的解决方法</p> <div class="inster-summary text-muted"> 当你发现代码相同,使用 c++ 和 Java 分别提交,c++ 通过了而 Java 却没有,可以采取以下方法…… </div> </div> </a> <!-- .inner-content #####--> </div> <!-- .post-inser ####--> </div> 优化后代码: ``` import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Main { public static void main(String[] args) throws IOException { int N = Reader.nextInt(); int[] arr = new int[N]; int min = 1000000, max = 0, m = 0, n = 0; for (int i = 0; i < arr.length; i++) { arr[i] = Reader.nextInt(); if (min > arr[i]) min = arr[i]; if (max < arr[i]) max = arr[i]; } for (int i : arr) { if (min == i) m++; if (max == i) n++; } System.out.print(min + " " + m + "\n" + max + " " + n); } } class Reader { static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); static StringTokenizer tokenizer = new StringTokenizer(""); static String nextLine() throws IOException {// 读取下一行字符串 return reader.readLine(); } static String next() throws IOException {// 读取下一个字符串 while (!tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } static int nextInt() throws IOException {// 读取下一个int型数值 return Integer.parseInt(next()); } static double nextDouble() throws IOException {// 读取下一个double型数值 return Double.parseDouble(next()); } } ``` © From the Internet Like 0 If you think my article is useful to you, please feel free to appreciate