解题思路: 排序,取第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 人评分