When Dr. Orooji was your age, one of the popular TV shows was “Get Smart!” The main character in this show (Maxwell Smart, a secret agent) had a few phrases; we used one such phrase for the title of this problem and we’ll use couple more in the output!
A “prime” number is an integer greater than 1 with only two divisors: 1 and itself; examples include 5, 11 and 23. Given a positive integer, you are to print a message indicating whether the number is a prime or how close it is to a prime.
输入
The first input line contains a positive integer, n (n ≤ 100), indicating the number of values to check. The values are on the following n input lines, one per line. Each value will be an integer between 2 and 10,000 (inclusive).
输出
At the beginning of each test case, output “Input value: v” where v is the input value.
Then, on the next output line, print one of the following two messages:
- If the number is a prime, print “Would you believe it; it is a prime!”
- If the number is not a prime, print “Missed it by that much (d)!” where d shows how close the number is to a prime number (note that the closest prime number may be smaller or larger than the given number).
Leave a blank line after the output for each test case. Follow the format illustrated in Sample Output.
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int a[105],b[10010];
int main()
{
int n,i,f=0,s,k=0;
scanf("%d",&n);
for (i=0; i<n; i++)
{
scanf("%d",&a[i]);
}
for (i=2; i<10010; i++)
{
f=0;
for (int j=2; j*j<=i; j++)
{
if (i%j==0)
{
f=1;
break;
}
}
if (f==0)
{
b[k]=i;
k++;
}
}
for (i=0; i<n; i++)
{
f=0;
for (int j=0; j<10010; j++)
{
if (a[i]==b[j])
{
f=1;
break;
}
if (a[i]>b[j]&&a[i]<b[j+1])
{
if (a[i]-b[j]<b[j+1]-a[i])
s=a[i]-b[j];
else
s=b[j+1]-a[i];
break;
}
}
if (f==1)
{
printf("Input value: %d\n",a[i]);
printf("Would you believe it; it is a prime!\n");
}
else
{
printf("Input value: %d\n",a[i]);
printf("Missed it by that much (%d)!\n",s);
}
printf("\n");
}
return 0;
}
本来不想记的 你出那么多次 每次都得重新打一遍 我就记咯