问题
1061 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
思路
字符数组存储这四个字符串,前两个为一组,后两个为一组。分别对每组中的一个字符串进行遍历,用if进行判断,筛选符合题目要求的字符,按要求输出。
代码
#include<bits/stdc++.h>
using namespace std;
char s1[65],s2[65],s3[65],s4[65];
int main(){
scanf("%s%s%s%s",s1,s2,s3,s4);
int i;
for (i = 0; i < 60; ++i) {
if(s1[i]>='A' && s1[i]<='G'){
if(s1[i] == s2[i]){
switch (s1[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;
}
cout<<" ";
break;
}
}
}
for (int k = i+1; k < 60; ++k) {
if(s1[k]>='0' && s1[k]<='9'){
if(s1[k] == s2[k]){
printf("0%c",s1[k]);
break;
}
}
else if(s1[k]>='A' && s1[k]<='N'){
if(s1[k] == s2[k]){
printf("%d",s1[k]-'A'+10);
break;
}
}
}
printf(":");
for (int j = 0; j < 60; ++j) {
if((s3[j]>='a' && s3[j]<='z') || (s3[j]>='A' && s3[j]<='Z')){
if(s3[j]==s4[j]){
printf("%02d\n",j);
break;
}
}
}
return 0;
}
总结
此题特别要注意,找到第一个符合题意的字符后要用break停止循环,因为字符串中可能还存在第二个符合要求的字符,如果不及时停止遍历可能会出错。