(PAT 1061) Dating (字符串处理)

刺骨的言语ヽ痛彻心扉 2022-03-21 04:14 240阅读 0赞

1061 Dating (20 point(s))

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:

  1. 3485djDkxh4hhGE
  2. 2984akDfkkkkggEdsb
  3. s&hgsfdk
  4. d&Hyscvnm

Sample Output:

  1. THU 14:04

解题思路:

算法比较简单,字符串处理筛选字符就行,难在范围的控制上

日期范围:A-G (大写字母)

小时范围:0-9 A-N (大写字母)

分钟范围:大小写字母

  1. #include <iostream>
  2. #include <algorithm>
  3. #include <string>
  4. using namespace std;
  5. string Date[7] = { "MON","TUE","WED","THU","FRI","SAT","SUN" };
  6. int main() {
  7. string codes[4];
  8. for (int i = 0; i < 4; ++i) {
  9. cin >> codes[i];
  10. }
  11. int pointer1 = 0;
  12. int pointer2 = 0;
  13. int pointer3 = 0;
  14. int pointer4 = 0;
  15. int num1 = 0, num2 = 0, num3 = 0;
  16. while (pointer1 < codes[0].length() && pointer2 < codes[1].length()) {
  17. if (isupper(codes[0][pointer1]) && isupper(codes[1][pointer2]) && codes[0][pointer1] <= 71 && codes[0][pointer1] == codes[1][pointer2]) {
  18. num1 = codes[0][pointer1] - 65;
  19. pointer1++;
  20. pointer2++;
  21. break;
  22. }
  23. pointer1++;
  24. pointer2++;
  25. }
  26. while (pointer1 < codes[0].length() && pointer2 < codes[1].length()) {
  27. if ((isupper(codes[0][pointer1]) && isupper(codes[1][pointer2]) && codes[0][pointer1] <= 78) || (isdigit(codes[0][pointer1]) && isdigit(codes[1][pointer2])) && codes[0][pointer1] == codes[1][pointer2]) {
  28. if (isdigit(codes[0][pointer1])) {
  29. num2 = codes[0][pointer1] - 48;
  30. }
  31. else {
  32. num2 = codes[0][pointer1] - 65 + 10;
  33. }
  34. break;
  35. }
  36. pointer1++;
  37. pointer2++;
  38. }
  39. while (pointer3 < codes[2].length() && pointer4 < codes[3].length()) {
  40. if (isalpha(codes[2][pointer3]) && isalpha(codes[3][pointer4]) && codes[2][pointer3] == codes[3][pointer4]) {
  41. num3 = pointer3;
  42. break;
  43. }
  44. pointer3++;
  45. pointer4++;
  46. }
  47. printf("%s %02d:%02d", Date[num1].c_str(), num2, num3);
  48. return 0;
  49. }

发表评论

表情:
评论列表 (有 0 条评论,240人围观)

还没有评论,来说两句吧...

相关阅读

    相关 PAT乙级1061

    1061 判断题 (15 分) 判断题的评判很简单,本题就要求你写个简单的程序帮助老师判题并统计学生们判断题的得分。 输入格式: 输入在第一行给出两个不超过 100