Supreme Number(找规律)(ACM-ICPC计蒜客)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/a17865569022/article/details/82530601

A prime number (or a prime) is a natural number greater than
1that cannot be formed by multiplying two smaller natural
numbers.

Now lets define a number N as the supreme number if and only if each number made up of an non-empty subsequence of all the numeric digits of N must be either a prime number or 1.

For example, 17 is a supreme number because 1, 7, 17 are all prime numbers or 1, and 19 is not, because 9 is not a prime number.

Now you are given an integer N (2 ≤ N ≤ 10100 ), could you find the maximal supreme number that does not exceed N?

Input
In the first line, there is an integer T (T ≤ 100000) indicating the numbers of test cases.

In the following T lines, there is an integer N (2 ≤ N ≤ 10100 ).

Output

For each test case print “Case #x: y” , in which x is the order number of the test case and y is the answer.
样例输入
2
6
100
样例输出
Case #1: 5
Case #2: 73

题意:给出一个n (2 ≤ N ≤ 10100 ),找到最接近且小于n的一个数,这个数需要满足每位上的数字构成的集合的每个非空子集组成的数字是个素数或1。
打表可以找出前19个符合要求的数字,并且在大于317之后的都不符合。

#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
int p[]={1,2,3,5,7,11,13,17,23,31,37,53,71,73,113,131,137,173,311,317};
int T,num;
string st;
int main()
{
    scanf("%d",&T);
    for(int t=1;t<=T;t++)
    {
        num=0;
        cin>>st;
        if(st.length()>=4)
            printf("Case #%d: %d\n",t,p[19]);
        else
        {
            for(int i=0;i<st.length();i++)
            {
                num*=10;
                num+=st[i]-'0';
            }
            for(int i=19;i>=0;i--)
            {
                if(p[i]<=num)
                {
                    printf("Case #%d: %d\n",t,p[i]);
                    break;
                }
            }
        }
        st="";
    }
}

猜你喜欢

转载自blog.csdn.net/a17865569022/article/details/82530601