int l,r,num,abc=0; cin >> l >> r; int res[10000]; for (int a = 1; a <= 9; a += 2)//一位 { num =a; if (num >= l && num <= r && isp(num)) { res[abc] = num; abc++; } } for (int a = 0; a <= 9; a++)//三位 { for (int b = 1; b <= 9; b += 2) { num =b * 100 + a * 10 + b; if (num >= l && num <= r && isp(num)) { res[abc] = num; abc++; } } } for (int a = 0; a <= 9; a++)//五位 { for (int b = 0; b <= 9; b++) { for (int c = 1; c <= 9; c += 2) { num = c * 10000 + b * 1000 + a * 100 + b * 10 + c; if (num >= l && num <= r && isp(num)) { res[abc] = num; abc++; } } } } for (int a = 0; a <= 9; a++)//七位 { for (int b = 0; b <= 9; b++) { for (int c = 0; c <= 9; c++) { for (int d = 1; d <= 9; d+=2) { num = d * 1000000 + c * 100000 + b * 10000 + a * 1000 + b * 100 + c * 10 + d ; if (num >= l && num <= r && isp(num)) { res[abc] = num; abc++; } } } } } for (int a = 0; a <= 9; a++)//九位 { for (int b = 0; b <= 9; b++) { for (int c = 0; c <= 9; c++) { for (int d = 0; d <= 9; d++) { for (int e = 1; e <= 9; e+=2) { num = e * 100000000 + d * 10000000 + c * 1000000 + b * 100000 + a * 10000 + b * 1000 + c * 100 + d * 10 + e; if (num >= l && num <= r && isp(num)) { res[abc] = num; abc++; } } } } } }
质数判断函数:
1 2 3 4 5 6 7 8 9 10 11
intisp(int a) { for (int i = 2; i <= floor(sqrt(a));i++) { if (a % i == 0) { return0;//能被整除说明不是质数 } } return1; }
但是这样子储存在数组中的数据顺序有些乱,因为我是按照它中间那一位数的顺序循环的。所以需要排一下序:
1
sort(res, res + abc);
然后就珂以输出了:
1 2 3 4
for (int z = 0; z <= abc-1; z++) { cout << res[z] << endl; }
for (int b = 0; b <= 9; b++)//六位 { for (int c = 0; c <= 9; c++) { for (int d = 1; d <= 9; d += 2) { num = d * 100000 + c * 10000 + b * 1000 +b * 100 + c * 10 + d; if (num >= l && num <= r && isp(num)) { res[abc] = num; abc++; } } } } for (int c = 0; c <= 9; c++)//四位 { for (int d = 1; d <= 9; d += 2) { num = d * 1000 + c * 100 + c * 10 + d; if (num >= l && num <= r && isp(num)) { res[abc] = num; abc++; } } } for (int d = 1; d <= 9; d += 2)//两位 { num = d * 10 +d; if (num >= l && num <= r && isp(num)) { res[abc] = num; abc++; } }
usingnamespacestd; intisp(int a) { for (int i = 2; i <= floor(sqrt(a));i++) { if (a % i == 0) { return0; } } return1; }
intmain() { int l,r,num,abc=0; cin >> l >> r; int res[10000]; for (int a = 1; a <= 9; a += 2)//一位 { num =a; if (num >= l && num <= r && isp(num)) { res[abc] = num; abc++; } } for (int a = 0; a <= 9; a++)//三位 { for (int b = 1; b <= 9; b += 2) { num =b * 100 + a * 10 + b; if (num >= l && num <= r && isp(num)) { res[abc] = num; abc++; } } } for (int a = 0; a <= 9; a++)//五位 { for (int b = 0; b <= 9; b++) { for (int c = 1; c <= 9; c += 2) { num = c * 10000 + b * 1000 + a * 100 + b * 10 + c; if (num >= l && num <= r && isp(num)) { res[abc] = num; abc++; } } } } for (int a = 0; a <= 9; a++)//七位 { for (int b = 0; b <= 9; b++) { for (int c = 0; c <= 9; c++) { for (int d = 1; d <= 9; d+=2) { num = d * 1000000 + c * 100000 + b * 10000 + a * 1000 + b * 100 + c * 10 + d ; if (num >= l && num <= r && isp(num)) { res[abc] = num; abc++; } } } } } for (int a = 0; a <= 9; a++)//九位 { for (int b = 0; b <= 9; b++) { for (int c = 0; c <= 9; c++) { for (int d = 0; d <= 9; d++) { for (int e = 1; e <= 9; e+=2) { num = e * 100000000 + d * 10000000 + c * 1000000 + b * 100000 + a * 10000 + b * 1000 + c * 100 + d * 10 + e; if (num >= l && num <= r && isp(num)) { res[abc] = num; abc++; } } } } } } for (int b = 0; b <= 9; b++)//六位 { for (int c = 0; c <= 9; c++) { for (int d = 1; d <= 9; d += 2) { num = d * 100000 + c * 10000 + b * 1000 +b * 100 + c * 10 + d; if (num >= l && num <= r && isp(num)) { res[abc] = num; abc++; } } } } for (int c = 0; c <= 9; c++)//四位 { for (int d = 1; d <= 9; d += 2) { num = d * 1000 + c * 100 + c * 10 + d; if (num >= l && num <= r && isp(num)) { res[abc] = num; abc++; } } } for (int d = 1; d <= 9; d += 2)//两位 { num = d * 10 +d; if (num >= l && num <= r && isp(num)) { res[abc] = num; abc++; } } sort(res, res + abc); for (int z = 0; z <= abc-1; z++) { cout << res[z] << endl; } return0; }
intisp(int pr) { for (int i = 2; i <= floor(sqrt(pr)); i++) { if (pr % i == 0) { return0; } } return1; } intishui(int x) { int s=0; if (x % 10 == 0 || x <= 0) { return0; } while (x > s) { s = s * 10 + x % 10; x /= 10; } if (x == s || s / 10 == x) { return1; } else { return0; } } intmain() { int s = 0; int l=5, r=100000000; for (int a = 0; a <= 9; a++) { for (int b = 0; b <= 9; b++) { for (int c = 0; c <= 9; c++) { for (int d = 0; d <= 9; d++) { for (int e = 0; e <= 9; e++) { for (int f = 0; f <= 9; f++) { for (int g = 0; g <= 9; g++) { for (int h = 0; h <= 9; h++) { for (int i = 0; i <= 9; i++) { s = a * 100000000 + b * 10000000 + c * 1000000 + d * 100000 + e * 10000 + f * 1000 + g * 100 + h * 10 + i; if (isp(s) && ishui(s) && s >= l && s <= r) { cout << s << endl; } } } } } } } } } } return0; }