62,628
社区成员
发帖
与我相关
我的任务
分享
import java.util.Scanner;
public class Demo1 {
static int value, distance;
public static void main(String[] args) {
// TODO Auto-generated method stub
int s[][];
Scanner sc = new Scanner(System.in);
System.out.print("请输入要测试的数字数N(0<N<=10000):");
value = sc.nextInt();
s = new int[value][2];
for (int i = 0; i < value; i++) {
System.out.print("请输入要测试的数字数M(2<M<1000000):");
s[i][0] = sc.nextInt();
sushu(s[i][0], 0);
s[i][1] = distance;
}
for (int i = 0; i < value; i++) {
System.out.println((s[i][0] + s[i][1]) + "\t" + Math.abs(s[i][1]));
}
}
static void sushu(int x, int y) {
distance = y;
if (judge(x - distance)) {
distance = -distance;
} else if (!judge(x + distance)) {
distance++;
sushu(x, distance);
}
}
static boolean judge(int t) {
for (int i = 2; i <= Math.sqrt(t); i++) {
if (t % i == 0) {
return false;
}
}
return true;
}
}
public static boolean isValid(String s){
Stack<Character> stack = new Stack<>();
for(char c : s.toCharArray()){
if(c == '(')
stack.push(')');
else if(c == '{')
stack.push('}');
else if(c == '[')
stack.push(']');
else if(stack.isEmpty() || (char)stack.pop() != c)
return false;
}
return stack.isEmpty();
}