PAT BASIC LEVEL 1014. 福尔摩斯的约会 (20)
2017-01-10 15:02
441 查看
1014. 福尔摩斯的约会 (20)
大侦探福尔摩斯接到一张奇怪的字条:“我们约会吧! 3485djDkxh4hhGE 2984akDfkkkkggEdsb s&hgsfdk d&Hyscvnm”。大侦探很快就明白了,字条上奇怪的乱码实际上就是约会的时间“星期四 14:04”,因为前面两字符串中第1对相同的大写英文字母(大小写有区分)是第4个字母’D’,代表星期四;第2对相同的字符是’E’,那是第5个英文字母,代表一天里的第14个钟头(于是一天的0点到23点由数字0到9、以及大写字母A到N表示);后面两字符串第1对相同的英文字母’s’出现在第4个位置(从0开始计数)上,代表第4分钟。现给定两对字符串,请帮助福尔摩斯解码得到约会的时间。输入格式:
输入在4行中分别给出4个非空、不包含空格、且长度不超过60的字符串。
输出格式:
在一行中输出约会的时间,格式为“DAY HH:MM”,其中“DAY”是某星期的3字符缩写,即MON表示星期一,TUE表示星期二,WED表示星期三,THU表示星期四,FRI表示星期五,SAT表示星期六,SUN表示星期日。题目输入保证每个测试存在唯一解。
输入样例:
3485djDkxh4hhGE
2984akDfkkkkggEdsb
s&hgsfdk
d&Hyscvnm
输出样例:
THU 14:04
Answer:
#include<iostream> using namespace std; bool is_upper(char c, char end) { return c >= 'A' && c <= end; } bool is_lower(char c) { return c >= 'a' && c <= 'z'; } bool is_alpha(char c) { return is_upper(c, 'Z') || is_lower(c); } bool is_digit(char c) { return c >= '0' && c <= '9'; } int get_day(char* str1, char* str2, int* index) { while(str1[*index] && str2[*index]) { if(str1[*index] == str2[*index]) if(is_upper(str1[*index], 'G')) return str1[*index] - 'A'; (*index) ++; } } int get_hour(char* str1, char* str2, int* index) { while(str1[*index] && str2[*index]) { if(str1[*index] == str2[*index]) { if(is_upper(str1[*index], 'N')) { return str1[*index] - 'A' + 10; } else if(is_digit(str1[*index])) { return str1[*index] - '0'; } } (*index) ++; } } int get_minute(char* str3, char* str4) { int index = 0; while(str3[index] && str4[index]) { if(str3[index] == str4[index] && is_alpha(str3[index])) return index; index ++; } } int main() { char input[4][61]; for(int i = 0; i < 4; i ++) { cin >> input[i]; } int index = 0; int day = get_day(input[0], input[1], &index); index ++; int hour = get_hour(input[0], input[1], &index); int minute = get_minute(input[2], input[3]); switch(day) { case 0:cout << "MON ";break; case 1:cout << "TUE ";break; case 2:cout << "WED ";break; case 3:cout << "THU ";break; case 4:cout << "FRI ";break; case 5:cout << "SAT ";break; case 6:cout << "SUN ";break; } cout << hour/10 << hour%10 << ':' << minute/10 << minute%10; }
PS.
最后把判断大写字母的函数
is_upper()加上末位限制于是通过,不知为何有点恼火。
因此把获取
day、
hour、
minute的代码抽取成了函数。
(我是笨蛋吧。)
相关文章推荐
- PAT (Basic Level) Practise (中文)- 1014. 福尔摩斯的约会 (20)
- PAT (Basic Level) Practise (中文) 1014. 福尔摩斯的约会 (20)
- PAT(basic level) 1014 福尔摩斯的约会(20)
- 【C++】浙大PAT (Basic Level)1014. 福尔摩斯的约会 (20)
- PAT Basic Level 1014. 福尔摩斯的约会(20)
- PAT (Basic Level) Practise (中文)1014. 福尔摩斯的约会 (20)
- PAT (Basic Level) Practise 1014. 福尔摩斯的约会 (20)
- PAT (Basic Level) Practise (中文)1014. 福尔摩斯的约会 (20)
- PAT乙级(Basic Level)真题1004 福尔摩斯的约会 (20)
- PAT (Basic Level) Practise:1014. 福尔摩斯的约会
- PAT乙级(Basic Level)真题-1004 福尔摩斯的约会 (20)
- PAT(Basic Level)_1014_福尔摩斯的约会
- PAT BASIC LEVEL 1014福尔摩斯的约会
- 【再思考】PATBasic——1014. 福尔摩斯的约会 (20)
- PAT Basic 1014. 福尔摩斯的约会 (20)(C语言实现)
- PAT(BasicLevel):福尔摩斯的约会 (20)
- PAT (Basic Level) Practise (中文)1014.福尔摩斯的约会
- PAT1014 BASIC:福尔摩斯的约会 (20)
- PAT basic-level 1014 福尔摩斯的约会 笔记
- PAT乙级真题及训练集 PAT (Basic Level) Practise (中文)1014. 福尔摩斯的约会