7-1 Sexy Primes (20 分)
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 (≤108).
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
思路分析
这是19年3月甲级的第1题,也是我当时的考题,9月1号教育超市重新做了一回。
当时得了15分,当时题目都没怎么看懂pair都不认识,看了下题目集发现练判素函数都写错了
这次得了20分,判素函数依旧写错了,可能是这个原因
最小的素数为2 最小的素数为2 最小的素数为2
9月8号题目更新之后再交着看看
#include<cstdio>
#include<iostream>
#define MAX 100000010
using namespace std;
bool isPrime(int n)
{
if(n==0||n==1)return false;
for(int i=2;i*i<=n;i++)
{
if(n%i==0)return false;
}
return true;
}
bool isSex(int n)
{
if(n>6)if(isPrime(n)&&isPrime(n-6))return true;//n is positive
if(isPrime(n)&&isPrime(n+6))return true;
return false;
}
int main()
{
int n;cin>>n;
if(isSex(n))
{
cout<<"Yes"<<endl;
if(isPrime(n-6))cout<<n-6;
else cout<<n+6;
}
else
{
cout<<"No"<<endl;
for(int i=n+1;i<MAX;i++)
{
if(isSex(i))
{
cout<<i;
break;
}
}
}
}