1168 Prime Day(20)
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
复习一下string的截取操作str.substr(begin_index,end_index);
以及string to int 操作stoi(str);
1 #include<bits/stdc++.h> 2 using namespace std; 3 bool isPrime(int num){ 4 if (num==1) 5 return false; 6 if (num==2) 7 return true; 8 for(int i=2;i<=num/2;++i){ 9 if (num%i==0) 10 return false; 11 } 12 return true; 13 } 14 int main(){ 15 string input; 16 cin>>input; 17 int count=0; 18 for (int i=0;i<input.size();++i){ 19 string str_temp=input.substr(i,input.size()); //常用string截取函数 20 int int_temp=stoi(str_temp); //常用string转数字函数 21 cout<<str_temp<<" "; 22 if (isPrime(int_temp)){ 23 cout<<"Yes"<<endl; 24 } 25 else{ 26 cout<<"No"<<endl; 27 count++; 28 } 29 } 30 if (count==0){ 31 cout<<"All Prime!"<<endl; 32 } 33 }
浙公网安备 33010602011771号