1061 Dating (20 分)
1. 题目
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
2. 题意
题目给出四个字符串,要求从这四个字符串密文中找出时间信息。
前两个字符串:
- 找到两个字符串下标位置相同,对应字符相同,且字符为
A~G
(区分大小写)中其中一个,则该字符为星期几
的信息,对应转化为周一~周天。 - 从上面找到位置下一位开始,继续找下标相同,对应字符相同,且字符为
0~9或A~N
(区分大小写)其中一个,将该字符转化为小时
的信息,对应转化为0~23
小时。
后两个字符串:
- 找到两个字符串下标位置相同,对应字符相同,且字符必须为
数字或英文字母
(不区分大小写),将该字符对应的下标转化为分钟
信息。
3. 思路——字符串
-
我觉得这题难点主要是理解题意,理解后实现就不难了。
-
总结一下
<ctype.h>或<cctype>
头文件中的一些方法:int isalnum (int c)
:检查参数c是否为英文字母或阿拉伯数字。int isalpha (int c)
:检查参数c是否为英文字母。int isdigit(int c)
:检查参数c是否为阿拉伯数字0到9。int islower(int c)
:检查参数c是否为小写英文字母。int isupper(int c)
:检查参数c是否为大写英文字母。
4. 代码
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
string str1, str2, str3, str4;
int main()
{
cin >> str1 >> str2 >> str3 >> str4;
int minLen = min(str1.length(), str2.length());
int i;
// 首先找到前两个字符串中对应位置字符相同且字符为A~G其中一个,将其转化为时间星期几
for (i = 0; i < minLen; ++i)
{
if (str1[i] == str2[i] && str1[i] >= 'A' && str1[i] <= 'G')
{
switch (str1[i])
{
case 'A': cout << "MON"; break;
case 'B': cout << "TUE"; break;
case 'C': cout << "WED"; break;
case 'D': cout << "THU"; break;
case 'E': cout << "FRI"; break;
case 'F': cout << "SAT"; break;
case 'G': cout << "SUN"; break;
}
break;
}
}
// 再找出前两个字符串中对应位置字符相同(第2次相同),且字符为0~9或A~N其中一个,将其转化为小时
for (++i; i < minLen; ++i)
{
if (str1[i] == str2[i])
{
if (isdigit(str1[i]))
{
printf(" %02d:", str1[i] - '0');
break;
} else if (str1[i] >= 'A' && str1[i] <= 'N')
{
printf(" %02d:", str1[i] - 'A' + 10);
break;
}
}
}
minLen = min(str3.length(), str4.length());
// 最后找后两个字符串中对应位置相同的字符(字符要求为数字或英文字母),并将其下标转化为分钟
for (i = 0; i < minLen; ++i)
{
if (str3[i] == str4[i] && isalpha(str3[i]))
{
printf("%02d\n", i);
break;
}
}
return 0;
}