zoukankan      html  css  js  c++  java
  • 7-1 Prime Day (20 分)

    wbfg.JPG

    The above picture is from Sina Weibo, showing May 23rd, 2019 as a very cool "Prime Day". That is, not only that the corresponding number of the date 20190523 is a prime, but all its sub-strings ended at the last digit 3 are prime numbers.

    Now your job is to tell if a given date is a Prime Day.

    Input Specification:

    Each input file contains one test case. For each case, a date between January 1st, 0001 and December 31st, 9999 is given, in the format yyyymmdd.

    Output Specification:

    For each given date, output in the decreasing order of the length of the substrings, each occupies a line. In each line, print the string first, followed by a space, then Yes if it is a prime number, or No if not. If this date is a Prime Day, print in the last line All Prime!.

    Sample Input 1:

    20190523
     

    Sample Output 1:

    20190523 Yes
    0190523 Yes
    190523 Yes
    90523 Yes
    0523 Yes
    523 Yes
    23 Yes
    3 Yes
    All Prime!
     

    Sample Input 2:

    20191231
     

    Sample Output 2:

    20191231 Yes
    0191231 Yes
    191231 Yes
    91231 No
    1231 Yes
    231 No
    31 Yes
    1 No


    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=1010;
    int toInt(string st){
        int sum=0;
        for(int i=0;i<st.size();i++){
            sum=sum*10+(st[i]-'0');
        }
        return sum;
    }
    bool isP(int n){
        if(n<=1){
            return false;
        }
        for(int i=2;i<=sqrt(n);i++){
            if(n%i==0){
                return false;
            }
        }
        return true;
    }
    
    int main(){
        string s,str;
        cin>>s;
        bool flag=true;;
        int len=s.size();
        for(int i=0;i<len;i++){
            str=s.substr(i,len);
            int sum=toInt(str);
            if(isP(sum)){
                printf("%s Yes
    ",str.c_str());
            }
            else{
                printf("%s No
    ",str.c_str());
                flag=false;
            }
        }
        if(flag==true){
            printf("All Prime!
    ");
        }
        return 0;
    }
  • 相关阅读:
    谷歌地图API学习
    aspx net.2.0 C#获取IP,URL,浏览器,操作系统
    FLASH+Javascript 1,2,3,4数字标签显示图片
    yui cookie Dynamically Change Text Size Using Javascript 动态设置字体大小,写入Cookie
    [转]控制 Cookie 的作用范围
    C# 关于URL地址操作
    C#_采集
    关于C#_ArrayList的两篇文章
    未能找到存储过程_master.dbo.xp_regread
    [转]C#泛型有什么好处(转)
  • 原文地址:https://www.cnblogs.com/dreamzj/p/14508292.html
Copyright © 2011-2022 走看看