PAT-A 1061 Dating (20 分)

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 SUNfor 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

思路:

1.从前面两字符串中,找到第1对相同的大写英文字母(A~G),得到日期。第4个字母'D',代表星期四

2.从前面两字符串中,找到第2对相同的符合条件的字符(0~9或者是A~Z),得到小时。'E',是第5个英文字母,代表一天里的第14个钟头(于是一天的0点到23点由数字0到9、以及大写字母A到N表示),这里注意从第一对符合条件的字母后面的位置开始寻找。

3.从后面两字符串中,找到第一对相同的英文字母在字符串中的位置,为分钟。

#include <cstdio>
#include <cctype>

int main()
{
    char in[4][65];//存储输入的字符串
    for(int i = 0; i < 4; i++)
        scanf("%s", in[i]);

    char result[2];//保存输入字符串中代表日期和小时的字符,最后根据字符进行转换
    int i;
    for(i = 0; i < 60; i++)
    {   //合法表示日期的字为大写的A~N
        if(in[0][i] == in[1][i] && in[0][i] >= 'A' && in[0][i] <= 'G')
        {
            result[0] = in[0][i];//保存表示日期的字符
            break;
        }
    }

    for(i++; i < 60; i++)//i++保证在第一对相等的合法字符后进行搜索
    {   //合法的表示小时的字符为0~9或者大写A~N
        if(in[0][i] == in[1][i] && (isdigit(in[1][i]) || in[0][i] >= 'A' && in[0][i] <= 'N'))
        {
            result[1] = in[0][i];//保存表示小时的字符
            break;
        }
    }

    int minute;//表示分钟
    for(i = 0; i < 60; i++)
    {   //合法的表示分钟的字符为英文字母
        if(in[2][i] == in[3][i] && isalpha(in[2][i]))
        {
            minute = i;
            break;
        }
    }

    char *days[7] = {"MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"};
    int day = result[0] - 'A';//表示日期的字符串的位置
    int hour = isdigit(result[1]) ? result[1] - '0' : result[1] - 'A' + 10;//计算小时数

    printf("%s %02d:%02d", days[day], hour, minute);
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值