【PAT甲级 约会】1061 Dating (20 分) C++ 全部AC

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/sinat_42483341/article/details/100567464

题目

这个题要注意的细节很多。因为一个星期有七天,一天有24小时,所以要注意字母范围这个隐藏条件,不符合条件的字母要跳过。

还要注意:第二次查找,要接着第一次找到的位置开始找。这个有点坑,题目里没说明白,只说是第二个相同的字符,我一开始是从头找的,所以这个点卡了很久。

所以,做题还是要先读懂题再开始敲,不然的话,用错误的理解敲出来再一点一点改太浪费时间了。
在这里插入图片描述
自己用的测试用例,用于测试第二次查找的开始要接着第一次的结束位置继续找这个坑

s99ssssssssccccAE432543
s99ssssssssccccAE123123
s&hgifdknn
d&Hyscvnmnm

答案:MON 14:09

题解 C++

#include<iostream>
#include<string>
using namespace std;
//这个题要注意的细节很多。因为一个星期有七天,一天有24小时,所以要注意字母范围这个隐藏条件,不符合条件的字母要跳过。
int main() {
	string str1, str2, str3, str4;
	cin >> str1 >> str2 >> str3 >> str4;

	int weekday = -1;
	int hour = -1;
	//在前两个字符串中,找第一个相同的大写字母(由于一周七天的约束,应该在A-G范围内)
	int temp = -1;
	int len = str1.length() < str2.length() ? str1.length() : str2.length();
	for (int i = 0; i < len; i++) {
		if (str1[i] == str2[i] && str1[i] >= 'A'&&str1[i] <= 'G') {
			weekday = str1[i] - 'A';
			temp = i;
			break;
		}
	}
	//接着上次找到的位置继续找,找到第一个相同的数字(0-9)或大写字母(A-N)
	for (int i = temp + 1; i < len; i++) {
		if (str1[i] == str2[i]) {
			if (str1[i] >= '0'&&str1[i] <= '9') {//是数字
				hour = str1[i] - '0';
				break;
			}
			else if (str1[i] >= 'A'&&str1[i] <= 'N') {//是大写字母
				hour = 10 + (str1[i] - 'A');
				break;
			}
		}
	}

	//后两个字符串中,找到第一个相同字母的位置
	int minute = -1;
	len = str3.length() < str4.length() ? str3.length() : str4.length();
	for (int i = 0; i < len; i++) {
		if (str3[i] == str4[i]) {
			if ((str3[i] >= 'A'&&str3[i] <= 'Z') || (str3[i] >= 'a'&&str3[i] <= 'z')) {
				minute = i;
				break;
			}
		}
	}

	string weekdays[] = { "MON","TUE","WED","THU","FRI","SAT","SUN" };
	cout << weekdays[weekday] << " ";
	printf("%02d:%02d", hour, minute);
	system("pause");
}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/sinat_42483341/article/details/100567464