Sexy primes are pairs of primes of the form (p, p+6), so-named since “sex” is the Latin word for “six”. (Quoted from http://mathworld.wolfram.com/SexyPrimes.html)
Now given an integer, you are supposed to tell if it is a sexy prime.
Input Specification:
Each input file contains one test case. Each case gives a positive integer N (≤10).
Output Specification:
For each case, print in a line Yes if N is a sexy prime, then print in the next line the other sexy prime paired with N (if the answer is not unique, output the smaller number). Or if N is not a sexy prime, print No instead, then print in the next line the smallest sexy prime which is larger than N.
Sample Input 1:
47
Sample Output 1:
Yes
41
Sample Input 2:
21
Sample Output 2:
No
23
正确做法
#include <iostream>
#include <cmath>
using namespace std;
bool isPrime(int x)
{
if (x <= 1) return false;
for(int i = 2; i <= x / i; i++)
{
if (x % i == 0) return false;
}
return true;
}
int main() {
int x;
cin >> x;
if ((isPrime(x) && isPrime(x - 6))) {
printf("Yes\n");
printf("%d", x - 6);
} else if (isPrime(x) && isPrime(x + 6)) {
printf("Yes\n");
printf("%d", x + 6);
} else {
printf("No\n");
while (1) {
x++;
if (isPrime(x) && (isPrime(x + 6) || isPrime(x - 6))) break;
}
printf("%d", x);
}
return 0;
}
缺少判断,在while循环的时候
#include <cstring>
#include <iostream>
using namespace std;
const int N = 110;
bool res = true;
bool is_prime(int x)
{
for(int i = 2; i <= x / i; i++)
{
if (x % i == 0) return false;
}
return true;
}
int check(int m)
{
if (is_prime(m))
{
bool f1 = is_prime(m - 6);
bool f2= is_prime(m + 6);
if (f1) return m - 6;
else if (f2) return m + 6;
else{
m++;
res = false;
while(true)
{
bool f1 = is_prime(m);
bool f2 = is_prime(m - 6);
bool f3 = is_prime(m + 6);
if (f1 && f2) return m;
else if (f1 && f2) return m;
m++;
}
}
}else{
m++;
res = false;
while(true)
{
bool f1 = is_prime(m);
bool f2 = is_prime(m - 6);
bool f3 = is_prime(m + 6);
if (f1 && f2) return m;
else if (f1 && f2) return m;
m++;
}
}
}
int main()
{
int m;
cin >> m;
int k = check(m);
if (res) puts("Yes");
else puts("No");
printf("%d", k);
}
这标题太骚了 sexy + prime(数学生已沉迷