PAT (Basic Level) Practice 1014 福尔摩斯的约会 & PAT (Advanced Level) Practice 1061 Dating

一、概述

给出四段字符串s1,s2,s3,s4,按如下顺序解读:

s1和s2的第 1 对相同的大写英文字母表示星期;

s1和s2的第 2 对相同的字符表示小时;

s3和s4的第 1 对相同的英文字母表示分钟。

坑很多:

首先,“第一对相同的”,保证位置相同,字符相同;

然后,代表星期的字符必须限定是A~G,不能是A~Z;

同理,代表小时的字符必须限定是A~N;

最后,代表分钟的字符可以是大写,也可以是小写,必须全都写上。

二、分析

算法上没有什么好说的,只需注意一点,字母转化为星期时,使用字符串数组较好,字符串数组有两种方法:

其一:

char Week[7][5] = { "MON","TUE","WED","THU","FRI","SAT","SUN" };

注意第二维不能是3,因为是用三个大写字母的字符串,所以有一个结尾的‘\0’,至少需要四个,另外,初始化后数组其他元素也是‘\0’,这和初始化为{ 0 }是相同的。

其二:

char *Week[] = { "MON","TUE","WED","THU","FRI","SAT","SUN" };

不用管维数,很方便。

难点主要在于限制条件不多不少,我就把s3和s4中的数字也加入了比较,这样是错误的,题目中已经明确了是“英文字母”,所以不但不能算数字,而且要把大小写都算上。

三、总结

知识点有字符串数组怎么开,以及如何更好理解题意等。

PS:代码如下

#include
#include
#include

using namespace std;
//char *Week[] = { "MON","TUE","WED","THU","FRI","SAT","SUN" };
char Week[7][10] = { "MON","TUE","WED","THU","FRI","SAT","SUN" };
int main()
{
	string s1, s2, s3, s4;
	cin >> s1;
	cin >> s2;
	cin >> s3;
	cin >> s4;
	int ls1, ls2, ls3, ls4;
	ls1 = s1.length();
	ls2 = s2.length();
	ls3 = s3.length();
	ls4 = s4.length();
	int i, j;
	char Firstsame = -1, Secondsame = -1;
	int FirstsameS1 = 0;
	int m = 0, n = 0;
	for (i = 0; i < ls1; i++)
	{
		if(s1[i]==s2[i])
		{
			if (s1[i] >= 65 && s1[i] <= 71)
			{
				Firstsame = s1[i];
				FirstsameS1 = i;
				break;
			}
		}
	}
	for (i = FirstsameS1+1; i < ls1; i++)
	{
		if (s1[i] == s2[i])
		{
			if ((s1[i] >= 48 && s1[i] <= 57) || (s1[i] >= 65 && s1[i] <= 78))
			{
				Secondsame = s1[i];
				break;
			}
		}
	}
	int Thirdsame = -1;
	for (i = 0; i < ls3; i++)
	{
		if (s3[i] == s4[i])
		{
			if ((s3[i] >= 97 && s3[i] <= 122)|| (s3[i] >= 65 && s3[i] <= 90))
			{
				Thirdsame = i;
				break;
			}
		}
	}
	printf("%s ", Week[Firstsame - 65]);
	if (Secondsame >= 48 && Secondsame <= 57)
		printf("0%d:", Secondsame - 48);
	else
		printf("%d:", Secondsame - 55);
	if (Thirdsame >= 0 && Thirdsame <= 9)
		printf("0%d", Thirdsame);
	else
		printf("%d", Thirdsame);
}

 

你可能感兴趣的:(PAT)