大侦探福尔摩斯接到一张奇怪的字条:我们约会吧! 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
- Meaning of the title
Four strings: the first two strings get the date and hour of the appointment, the last two characters get the minute and
finally the expression of the date and time is output - Ideas
For the first two strings, the same letter appears in the same position, the first occurrence will determine the day of the week, the second will determine the hour; there may be the same later, but it is better to break directly, the
first one is A~G The letters represent MON-SUN, so pay attention to the constraints of traversal; the
second is 0~9 and AN, representing
the last two strings of 0-23 hours . It can be seen that the same letter is required, and the first occurrence The position represents the minute, so the maximum length is 60 characters.
Note the output format - Code
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
int main(){
int i,j,m,n;
string a,b,c,d;
char day,hour;//存储星期、小时,字符
int minu;//因为看位置得出分钟数,所以定义成int型比较简单
cin>>a>>b>>c>>d;
m=max(a.length(),b.length());//得到a、b字符串长度的最大值,以便后面循环遍历
//求星期几
for(i=0;i<m;i++){
if(a[i]==b[i]&&a[i]>='A'&&a[i]<='G'){
day=a[i];
j=i;
break;
}
}
//求小时
for(i=j+1;i<m;i++){
if(a[i]==b[i]&&((a[i]>='0'&&a[i]<='9')||(a[i]>='A'&&a[i]<='N'))){
hour=a[i];
break;
}
}
n=max(c.length(),d.length());//得到c、d字符串长度的最大值,以便后面循环遍历
//求分钟
for(i=0;i<n;i++){
if(c[i]==d[i]&&(c[i]>='a'&&c[i]<='z')||(c[i]>='A'&&c[i]<='Z')){
minu=i;
}
}
//将存储的星期字符转换成题目格式
switch(day){
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;
}
//将存储的小时字符转换成题目格式
if(hour<='9')
cout<<"0"<<hour<<":";
else
cout<<hour-55<<":"; //hour存储的是ABC字符,转换成10-23,需要-55
//将存储的分钟字符转换成题目格式
if(minu<10)
cout<<"0"<<minu;
else
cout<<minu;
return 0;
}
4. Summary This
question is not difficult, the key is to judge the constraints