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:
20190523
Sample Output:
20191231 Yes
0191231 Yes
191231 Yes
91231 No
1231 Yes
231 No
31 Yes
1 No
思路
字符串操作、数学题
cpp代码
#include<iostream>
using namespace std;
bool check(int x){
if(x<2)return false;
for(int i=2;i<=x/i;i++)
if(x%i==0)return false;
return true;
}
int main(){
string s;
cin>>s;
bool st=true;
for(int i=0;i<s.size();i++){
string str=s.substr(i);
int x=stoi(str);
if(check(x)){cout<<str<<" "<<"Yes"<<endl;}
else {
st=false;
cout<<str<<" "<<"No"<<endl;
}
}
if(st)puts("All Prime!");
return 0;
}