【最详细的分析】1061 Dating (20 分)
立志用最少的代碼做最高效的表達(dá)
PAT甲級最優(yōu)題解——>傳送門
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之間的字母。
判斷小時:如果是數(shù)字,那么在0-9之間, 如果是字母,那么在A-N之間,分別代表0-23點(diǎn)
判斷分鐘:必須是字母相等, 大寫小寫都可以, 可以用isalpha()函數(shù)判斷。
注意:
1、 小時和分鐘都要有前導(dǎo)0, 也就是如3小時要輸出03.
2、判斷星期后,要及時break或continue, 避免判斷星期的字母被當(dāng)成判斷小時的字母再讀一次(測試點(diǎn)二)
#include<bits/stdc++.h> using namespace std; int main() {//建立映射 unordered_map<char, string>week;unordered_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";for(char i = '0'; i <= '9'; i++) hour[i] = i-'0';for(char i = 'A', k = 10; i <= 'N'; i++, k++) hour[i] = k;string s1, s2, s3, s4;cin >> s1 >> s2 >> s3 >> s4;int len = min(s1.size(), s2.size());bool flag = false;for(int i = 0; i < len; i++) {if(s1[i] == s2[i]) if(!flag) { //輸出星期 if(week.count(s1[i]) > 0) {cout << week[s1[i]] << ' ';flag = true;}}else { //輸出小時 if(hour.count(s1[i]) > 0) {printf("%02d:", hour[s1[i]]); break; //切記切記, 輸出完小時候要及時break。 否則測試點(diǎn)2會報(bào)錯。 }}} int len1 = min(s3.size(), s4.size());//輸出字母 for(int i = 0; i < len1; i++) if(s3[i] == s4[i] && isalpha(s3[i])) printf("%02d", i);return 0; }
耗時:
求贊哦~ (?ω?)
總結(jié)
以上是生活随笔為你收集整理的【最详细的分析】1061 Dating (20 分)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【科学计数法模板讲解】1060 Are
- 下一篇: 【最后测试点超时】1063 Set Si