2018沈阳模拟赛-K

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

这道题很费劲的将所有superme number找了出来,发现317以后就再也没有该数了

1,2,3,5,7,11,13,17,23,31,37,53,71,73,113,131,137,173,311,317

这个过程过了

很久没写字符串的题,字符串的知识又糊了。输入一串很长的数,可以用一个char型数组n,直接scanf(“%s”,n);n[i]-'0'只能把某一位转化成整型数,不能把一串字符转化为整型数。

这个题有测试样例没通过,原来……当他大于等于317时,我少了个换行符…………

#include <iostream>
#include <cstring>

using namespace std;

int a[]={1,2,3,5,7,11,13,17,23,31,37,53,71,73,113,131,137,173,311,317};

int main(void) {
    int t,cases=0;
    cin >> t;
    while(t--){
        char n[101];
        int i, num=0;

        scanf("%s",n);
        i=strlen(n);

        cout<<"Case #"<<++cases<<": ";

        if(i>3)
            cout << "317";
        else{
            for(int j=0; j<i; j++)
            {
                num *= 10;
                num += (n[j] - '0');
            }
            int j;
            for( j=0; j<19; j++){
                if(a[j]<= num && a[j+1]>num)
                {
                    cout << a[j]<<endl;
                    break;
                 }

            }
            if(j==19) cout<<317<<endl;

        }

    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/han_hhh/article/details/82534328