浙江大学PAT_甲级_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
这题后来在乙级考试里重新出现过。

我的C++程序:

#include<iostream>
#include<string>
#include<map>
#include<iomanip>
using namespace std;
int main()
{
	int i = 0;
	string s1, s2, s3, s4;
	char week_sign,hour_sign;//week_sign第一对相同的大写字母代表星期,hour_sign代表小时的字符
	int minute=0;//分钟
	map<char, string>week;//星期几
	map<char,int>hour;//小时
	week['A'] = "MON";week['B'] = "TUE";week['C'] = "WED";week['D'] = "THU";
	week['E'] = "FRI";week['F'] = "SAT";week['G'] = "SUN";//从星期一到星期日
	hour['A'] = 10;hour['B'] = 11;hour['C'] = 12;hour['D'] = 13;hour['E'] = 14;
	hour['F'] = 15;hour['G'] = 16;hour['H'] = 17;hour['I'] = 18;hour['J'] = 19;
	hour['K'] = 20;hour['L'] =21;hour['M'] =22;hour['N'] =23;//从10小时到23小时
	cin >> s1 >> s2 >> s3 >> s4;//接受四个字符串
	int length = (s1.size() > s2.size() )? s2.size() : s1.size();//length是s1,s2里最短的字符长度
	for (i=0;i <length;i++)//得到代表星期几的大写字符
	{
		if (s1[i]>= 'A'&&s1[i]<= 'G'&&s1[i]==s2[i])//字符必须是大写字母
		{
			week_sign= s1[i];
			s1[i]= '@';
			break;//找到第一个就跳出循环
		}
	}
	for (;i < length;i++)//计算代表小时的字符
	{
		if (s1[i] == s2[i])
		{
			if (s1[i] >= '0'&&s1[i] <= '9')//'0'<=小时字符<='9'
			{
				hour_sign = s1[i];
				break;
			}
			if(s1[i] >= 'A'&&s1[i] <= 'N')//大写字母A-N表示10-23小时
			{
				hour_sign = s1[i];
				break;
			}
		}
	}
	length= (s3.size() > s4.size()) ? s4.size() : s3.size();//length是s3,s4里最短的字符长度
	for (i=0;i <length;i++)//计算分钟
	{
		if (s3[i] >= 'A'&&s3[i] <= 'z'&&s3[i] == s4[i])//分钟字符必须为为大写或小写字母,不是数字
		{
			minute =i;
			break;
		}
	}
	cout << week[week_sign] <<' ';//输出星期
	if (hour_sign >= '0'&&hour_sign <= '9')
		cout << 0 << hour_sign;
	else
		cout << hour[hour_sign];//输出小时
	cout<<':';
	cout << setw(2) << setfill('0') << minute;//输出两位的分钟,不足用0补满
	//system("pause");
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值