2020.7.25 PAT甲级7-1 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

题目大意: 给出一个日期,让你判断它,以及他每次减去一位后是否是素数。

分析: 从原始字符串中抠出子串转换成整数进行素数判断,简单模拟。
20分的题要熟练,动作快,20分钟内完成。

代码:

#include <bits/stdc++.h>
using namespace std;

bool isprime(int x){
	if(x < 2) return false;
	for(int i = 2; i * i < x; i ++ )
		if(x%i == 0) return false;
	return true; 
} 

int main(){
	bool flag = 1;
	string str;
	cin >> str;
	for(int i = 0; i < 8; i ++ ){
		string ans;
		ans = str.substr(i, 8-i); 	
		cout << ans << " " ;	
		
		if(isprime(stoi(ans))) cout << "Yes" << endl;
		else {
			cout << "No" << endl;
			flag = 0;
		}
	}

	if(flag) cout << "All Prime!" << endl;	
	
	return 0;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值