PAT甲级1061 Dating (20分)|C++实现

一、题目描述

原题链接
在这里插入图片描述

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

二、解题思路

稍微有点复杂的字符串处理题,搞清楚题目意思之后不难。前两个字符串的第一个相同大写字母对应周几,从这个字母开始,下一个相同的字符表示小时(这里题目the second common character可能表达得不是很好),后两个字符串的第一个相同字母所在的位置表示分钟。按这个逻辑设计程序即可。

三、AC代码

#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
using namespace std;
string day[7] = {
    
    "MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"};
int main()
{
    
    
    int d, hr, minute;
    string str1, str2, str3, str4;
    cin >> str1 >> str2 >> str3 >> str4;
    int sze1 = str1.length(), sze2 = str2.length(), sze3 = str3.length(), sze4 = str4.length();
    int min1 = min(sze1, sze2), min2 = min(sze3, sze4);
    int pos1 = -1, pos2, cntC = 0;
    for(int i=0; i<min1; i++)
    {
    
    
        if(str1[i] == str2[i])
        {
    
    
            if(cntC == 0 && str1[i]>='A' && str1[i]<='G')
            {
    
    
                cntC++;
                pos1 = i;
                continue;
            }
            if(pos1 != -1 && ((str1[i]>='0' && str1[i]<='9') || (str1[i]>='A' && str1[i] <= 'N')))
            {
    
    
                pos2 = i;
                if(str1[i]>='0'&&str1[i]<='9')  hr = str1[i] - '0';
                else    hr = str1[i] - 'A' + 10;
                break;
            }
        }
    }
    d = str1[pos1] - 'A' + 1;
    cout << day[d-1] << " ";
    if(hr >= 10) printf("%d:", hr);
    else    printf("0%d:", hr);
    for(int i=0; i<min2; i++)
    {
    
    
        if(str3[i] == str4[i] && ((str3[i] >= 'A' && str3[i] <= 'Z') || (str3[i] >= 'a' && str3[i] <= 'z')))
        {
    
    
            minute = i;
            break;
        }
    }
    printf("%02d\n", minute);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_42393947/article/details/108658715