PAT 乙级-1014(测试点2,4)

踩坑点:
代码不严谨所致,详见注释
第1 对相同的大写英文字母代表星期几,范围为A~G,而我却笼统地使用isupper判断,严谨的做法为str1[i] >= ‘A’ && str1[i] <=‘G’
第 2 对相同的字符代表小时,范围为0~9,A~N,笼统地使用isdigit(str1[i]) || isupper(str1[i])判断,严谨的做法为isdigit(str1[i]) || str1[i] >= ‘A’ && str1[i] <=‘N’

// 正确代码:
#include<iostream>
#include<cctype>
using namespace std;

int main() {
	string str1, str2, str3, str4, res = "";
	int flag = 0, pos;
	cin >> str1 >> str2 >> str3 >> str4;
	for(int i = 0; i < str1.length() && i < str2.length(); i++) {
		if(str1[i] == str2[i]){
			if(flag == 1 && (isdigit(str1[i]) || str1[i] >= 'A' && str1[i] <='N')) {
//错误代码:if(flag == 1 && (isdigit(str1[i]) || isupper(str1[i]))) {
				res += str1[i]; 
				break;
			} else if(str1[i] >='A' && str1[i] <= 'G') {
//错误代码:} else if(isupper(str1[i])) { 
				res += str1[i];
				flag = 1;
			}
		}
	}
	for(int i = 0; i < str3.length() && i < str4.length(); i++) {
		if(str3[i] == str4[i] && isalpha(str3[i])) {
			pos = i; break;
		}
	}
	string day[7] = {"MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"};
	int m = isdigit(res[1]) ? res[1] - '0' : res[1] - 'A' + 10;
	cout << day[res[0]-'A'] << " ";
	printf("%02d:%02d", m, pos);
	return 0;
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值