1061 Dating (20)

1061 Dating (20)(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

题目大意:给出四个字符串,前两个字符串确定日期和小时, 后两个字符串确定分钟;
     日期:前两个字符串第一个相等的大写字母(位置相同,字母相同)A~G 分别表示星期一到星期天
     小时:在找到日期的字符串后面继续查找,找到第一个相同的数字或字母(0~9,A~N)(位置也要相同),分别表示0~23
     分钟:在后面两个字符串中查找,第一个相等的字母,不分大小写, 字母相等的位置就是分钟
这里用cctype中的isalpha(),isdigit()判断字符是否为字母,或者数字。也可以用不等式来判断
 1 #include<iostream>
 2 #include<string>
 3 #include<cctype>
 4 using namespace std;
 5 int main(){
 6   string weekday[7]={"MON","TUE","WED","THU","FRI","SAT","SUN"};
 7   string a,b,c,d;
 8   int day=0, hour=0, min=0, cnt=0, i; 
 9   cin>>a>>b>>c>>d;
10   for(i=0; i<a.size() && i<b.size(); i++){
11       if(a[i]==b[i] && a[i]>='A' && a[i]<='G'){
12         day = a[i]-'A';
13         break;
14       }
15   }
16   for(i=i+1; i<a.size() && i<b.size(); i++){
17       if(a[i]==b[i]){
18           if(isdigit(a[i])) hour = a[i] - '0';
19           else if(a[i]>='A' && a[i]<='N') hour = a[i] - 'A' + 10;
20           else continue;
21           break;
22       }
23   }
24   for(int j=0; j<c.size() && j<d.size(); j++){
25       if(isalpha(c[j]) && c[j]==d[j]){
26         min = j;
27         break;
28       }
29   }
30   cout<<weekday[day];
31   printf(" %02d:%02d", hour, min);
32   return 0;
33 }

猜你喜欢

转载自www.cnblogs.com/mr-stn/p/9136504.html