解题思路: 排序,取第k大和第k小的数相减,判断是否为素数
import java.util.Arrays; import java.util.Scanner; public class C1268 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while(sc.hasNext()){ int n = sc.nextInt(), k = sc.nextInt(); int[] a = new int[n]; for(int i = 0; i < n; i++) a[i] = sc.nextInt(); Arrays.sort(a); int m = a[a.length-k] - a[k-1]; if(isSu(m)) System.out.printf("YES\n%d\n", m); else System.out.printf("NO\n%d\n", m); } sc.close(); } //判断是否为素数 private static boolean isSu(int n){ if(n <= 1) return false; for(int i = 2; i*i <= n; i++){ if(n % i == 0){ return false; } } return true; } }
注意事项:
参考代码:
0.0分
0 人评分
C语言程序设计教程(第三版)课后习题11.8 (C语言代码)浏览:807 |
汽水瓶 (C语言代码)浏览:599 |
2005年春浙江省计算机等级考试二级C 编程题(3) (C语言代码)浏览:389 |
C语言训练-数字母 (C语言代码)浏览:649 |
C语言程序设计教程(第三版)课后习题1.6 (C语言代码)浏览:655 |
【金明的预算方案】 (C++代码)浏览:841 |
1009题解浏览:746 |
C语言程序设计教程(第三版)课后习题5.5 (C语言代码)浏览:555 |
C二级辅导-等差数列 (C语言代码)浏览:695 |
交换Easy (C语言代码)浏览:761 |