PAT甲级1061答案(使用C语言)

那年仲夏 提交于 2020-02-17 09:24:22

题目要求

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

题目要求总结

这个题目要求输出两对字符串,也就是四个字符串,
第一对字符串:第一个相等的大写字母代表星期几(A-G表示),第二个相等的字母或者数字代表时间,(0-9和A - N表示)
第二对字符串:看第一个相等的字符的位置代表分钟

涉及到的有关字符的判别函数

需要刀肉ctype.h头文件

#include <ctype.h>
isalnum(int c) //sialpha或isdigit的字符
isalpha(int c) //isupper或islower的字符
isdigit(int c) //是否是数字
islower(int c) //是否为小写字母
isupper(int c) //是否为大写字母
tolower(int c) //转换为小写字母
toupper(int c) //转换成大写字母

AC代码

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
#include<ctype.h>
using namespace std;

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

int main(){
	char s1[1000], s2[1000], s3[1000], s4[1000];
	
	scanf("%s%s%s%s", s1, s2, s3, s4);
	int len1 = strlen(s1) > strlen(s2) ? strlen(s2):strlen(s1);
	int len2 = strlen(s3) > strlen(s4) ? strlen(s4):strlen(s3);
	int flag = 0;
	//printf("%d\n", len1);
	for(int i = 0; i < len1; i++){
		if(flag == 0 && s1[i]==s2[i] && s1[i] <= 'G' && s2[i] >= 'A'){
			printf("%s ", str_d[s1[i]-'A']);
			flag = 1;
			continue;
		}

		if(flag == 1 && s1[i]==s2[i] && (s1[i] >= 'A' && s1[i]<='N'||isdigit(s1[i]))){
			int x;
			if(isupper(s1[i]))
				x = s1[i] - 'A' +10;
			else
				x =  s1[i] - '0';
			printf("%02d:", x);
			break;
		}
	}
	for(int j = 0; j < len2; ++j){
		if(s3[j] == s4[j] && isalpha(s3[j])){
			printf("%02d\n", j);
			break;
		}
	}
	return 0;
}```

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!