UVA897 Anagrammatic Primes 题解
思路
我们首先看数据范围:让我失去了暴力的想法告诉我们不能暴力。
让我们再看看题目,可以发现反质数是不变的,就那么多,所以我们可以打表,求出
2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, 97, 113, 131, 199, 311, 337, 373, 733, 919, 991
之后就简单了,完整代码如下:
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <algorithm>
#include <iomanip>
using namespace std;
int n, a[50] = { 0, 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, 97, 113, 131, 199, 311, 337, 373, 733, 919, 991 };
int main(){
while (cin >> n) {
if (n == 0)break;
int m = 10 * n;
bool f = true;
for (int i = 1; i <= 22; i++) {
if (a[i] > n && a[i] < m) {
cout << a[i] << "\n";
f = false;
break;
}
}
if (f)cout << 0 << "\n";
}
return 0;
}
但是,这份代码竟然没过!在我用某度翻译原题面后发现了题目翻译的一个错误:“你必须找到大于
好了,接下来是完整 AC 代码:
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <algorithm>
#include <iomanip>
using namespace std;
int n, a[50] = { 0, 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, 97, 113, 131, 199, 311, 337, 373, 733, 919, 991 };//打表数组
int main(){
while (cin >> n) {
if (n == 0)break;
int k = 1, m = n;
while (m != 0)k *= 10,m/=10;//求10^n
bool f = true;
for (int i = 1; i <= 22; i++) {
if (a[i] > n && a[i] < k) {
cout << a[i] << "\n";//找到直接输出
f = false;
break;
}
}
if (f)cout << 0 << "\n";//没找到要输出0哦
}
return 0;//华丽结束
}
感谢收看我的题解能不能留个赞再走。