The above picture is from Sina Weibo and shows a very cool "Vegetarian Day": May 23, 2019. That is, not only 20190523 itself is a prime number, but any substring ending with the last number 3 is also a prime number.
This question asks you to write a program to determine whether a given date is a "full vegetarian day".
Input format:
The input gives a date in the format yyyymmdd. The guaranteed date of the question is between January 1, 0001 and December 31, 9999.
Output format:
Starting from the original date, in order of decreasing substring length, each line first outputs a substring and a space, and then outputs Yes, if the number corresponding to the substring is a prime number, otherwise it outputs No. If this date is an all-prime day, print All Prime! on the last line.
Input example 1:
20190523
Output sample 1:
20190523 Yes
0190523 Yes
190523 Yes
90523 Yes
0523 Yes
523 Yes
23 Yes
3 Yes
All Prime!
Input example 2:
20191231
Output sample 2:
20191231 Yes
0191231 Yes
191231 Yes
91231 No
1231 Yes
231 No
31 Yes
1 No
Problem-solving ideas:
Just complete it step by step
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAXN 10
bool isPrime(int n) {
if ( n < 2 ) return false;
for ( int i=2; i*i<=n; ++i )
if ( n % i == 0 ) return false;
return true;
}
int main(int argc, const char *argv[]) {
bool isAllPrime = true;
char date[MAXN] = {0,}, *p;
if ( scanf("%s", date)==EOF ) printf("error\n");
for ( p=date; *p; ++p ) {
printf("%s ", p);
if ( isPrime(atoi(p)) ) {
printf("Yes\n");
} else {
printf("No\n"); isAllPrime = false;
}
}
if ( isAllPrime ) printf("All Prime!\n");
return EXIT_SUCCESS;
}