Sorry About That, Chief!

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;
}

本来不想记的 你出那么多次 每次都得重新打一遍 我就记咯

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值