ACM-ICPC沈阳赛区预赛 K.Supreme Number

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

K.Supreme Number

复制黏贴有格式上的错误,特提供题目链接:https://nanti.jisuanke.com/t/31452

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

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

For example, 1717 is a supreme number because 11, 77, 1717 are all prime numbers or 11, and 1919 is not, because 99 is not a prime number.

Now you are given an integer N\ (2 \leq N \leq 10^{100})N (2≤N≤10100), could you find the maximal supreme number that does not exceed NN?

Input

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

In the following TT lines, there is an integer N\ (2 \leq N \leq 10^{100})N (2≤N≤10100).

Output

For each test case print "Case #x: y", in which xx is the order number of the test case and yy is the answer.

样例输入复制

2
6
100

样例输出复制

Case #1: 5
Case #2: 73

题目来源

ACM-ICPC 2018 沈阳赛区网络预赛

AC代码:

#include<iostream>
#include<cstdio>
#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 main(){
    int t,num;
    string str;
    scanf("%d",&t);
    for(int j=1;j<=t;j++){
        num=0;
        cin>>str;
        if(str.length()>=4)
            printf("Case #%d: %d\n",j,p[19]);
        else{
            for(int i=0;i<str.length();i++){
                num*=10;
                num+=str[i]-'0';
            }
            for(int i=19;i>=0;i--){
                if(p[i]<=num){
                    printf("Case #%d: %d\n",j,p[i]);
                    break;
                }
            }
        }
    }

}

解析:这道题目的主要意思是:我输入一个数,找出小于这个数的最大素数,还有一个要求就是找到的这个数进行分解我们需要保证每一位也都是素数。

         那么我们应该敏锐地观察到:题目给出的N的最大范围达到了10的100次方,我们应该想到用string(字符串)进行储存。

然后我们找出4位以内的所有符合要求的素数(其实就是打表的过程),而四位素数没有符合要求的,所以只要是输入的字符串长度大于等于4的我们输出的最大素数都是317,至于输入的其他小于四位的字符串,我们就需要先处理成实实在在的数字然后在素数数组中找到小于这个数字的最大素数进行输出。

       emmm其实思路还是挺简单的。。。

猜你喜欢

转载自blog.csdn.net/qq_37618760/article/details/82858557