PAT A1061 Dating (20 分)

Sherlock Holmes received a note with some strange strings: Let’s date! 3485djDkxh4hhGE 2984akDfkkkkggEdsb s&hgsfdk d&Hyscvnm. It took him only a minute to figure out that those strange strings are actually referring to the coded time Thursday 14:04 – since the first common capital English letter (case sensitive) shared by the first two strings is the 4th capital letter D, representing the 4th day in a week; the second common character is the 5th capital letter E, representing the 14th hour (hence the hours from 0 to 23 in a day are represented by the numbers from 0 to 9 and the capital letters from A to N, respectively); and the English letter shared by the last two strings is s at the 4th position, representing the 4th minute. Now given two pairs of strings, you are supposed to help Sherlock decode the dating time.

Input Specification:

Each input file contains one test case. Each case gives 4 non-empty strings of no more than 60 characters without white space in 4 lines.

Output Specification:

For each test case, print the decoded time in one line, in the format DAY HH:MM, where DAY is a 3-character abbreviation for the days in a week – that is, MON for Monday, TUE for Tuesday, WED for Wednesday, THU for Thursday, FRI for Friday, SAT for Saturday, and SUN for Sunday. It is guaranteed that the result is unique for each case.

Sample Input:

3485djDkxh4hhGE
2984akDfkkkkggEdsb
s&hgsfdk
d&Hyscvnm

Sample Output:

THU 14:04

题意:

给定4个字符串:
对于前2个字符串,出现的第一对相同的大写字母(A-G)表示星期一到星期日,第二对相同的字符(0-9||A-N)表示小时;
对于后2个字符串,出现的第一对字母(a-z||A-Z)表示分钟.

思路:

(1)开一个二维字符数组day存放日期;
(2)遍历前2个字符串,输出第一对相同的大写字母(A-G)所表示的日期和第二对相同的字符(0-9||A-N)所表示的小时;
(3)遍历后2个字符串,输出第一对字母(a-z||A-Z)所表示的分钟.

代码:

#include<cstdio>
char day[7][4]={"MON","TUE","WED","THU","FRI","SAT","SUN"};
char hh[24][3]={"00","01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23"};//int型不能赋值08、09,因为C++中数字0开头表示八进制数,故采用char型 
int main(){
	char a1[61],a2[61],b1[61],b2[61];
	scanf("%s%s%s%s",a1,a2,b1,b2);
	int i;
	for(i=0;i<60;i++){
		if(a1[i]==a2[i]&&a1[i]>='A'&&a1[i]<='G'){
			printf("%s ",day[a1[i]-'A']);
			break;
		}
	}
	for(++i;i<60;i++){
		if(a1[i]==a2[i]&&((a1[i]>='A'&&a1[i]<='N')||(a1[i]>='0'&&a1[i]<='9'))){
			if(a1[i]>='0'&&a1[i]<='9'){
				printf("%s:",hh[a1[i]-'0']);//也可采用下方 %02d 的方式直接输出a1[i]-'0' 
				break;
			}
			else{
				printf("%s:",hh[a1[i]-'A'+10]);
				break;
			}
		}
	}
	for(int j=0;j<60;j++){
		if(b1[j]==b2[j]&&((b1[j]>='a'&&b1[j]<='z')||(b1[j]>='A'&&b1[j]<='Z'))){
			printf("%02d",j);//数值长度不足2位就补0输出,但不影响长度超过2位的数值输出 
			break;
		} 
	}
	return 0;
}

词汇:

strange 奇怪的
coded 编码的
common capital English letter 共用的大写英文字母
case sensitive 对该情况敏感
shared 共享的,共用的
decode 译码
abbreviation 省略,缩写

发布了26 篇原创文章 · 获赞 0 · 访问量 480

猜你喜欢

转载自blog.csdn.net/PanYiAn9/article/details/102633382