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 Ato N, respectively); and the English letter shared by the last two strings is sat 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 DAYis a 3-character abbreviation for the days in a week – that is, MONfor Monday, TUEfor Tuesday, WEDfor Wednesday, THUfor Thursday, FRIfor Friday, SATfor 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

思路

这题比较简单,也是一个模拟题。主要步骤就是:

  • 在前两个字符串中到第一个相同位置相同字母,找到之后,字母在英文字母表中的顺序代表星期几(A代表星期一,B代表星期二……G代表星期天)。注意,这里一定要是【相同位置】,我一开始用map来找第一个相同字母和第二个相同字母,但是测试点1 2 4一直过不了,后来才看到,题目要求的是相同位置字母相同……
  • 在前两个字符串中到第二个相同位置相同字母或数字(AN或09),找到之后,它作为小时;
  • 在最后两个字符串找到第一个相同位置的字母(大小写字母都可),找到之后,它作为分钟。

坑点

  • 时钟和分钟如果不是两位数,都需要补“0”补到两位数。

代码

#include<cstdio>
#include<stdlib.h>
#include<algorithm>
#include<string.h>
#include<iostream>
using namespace std;
int main()
{
    string a, b, c, d, result;
    cin>>a>>b>>c>>d;
    int minlen = min(a.length(), b.length());
    int cnt = 0;
    for(int i=0;i<minlen;i++){
        if(a[i]==b[i]&&b[i]>='A'&&b[i]<='G'&&cnt==0){
            int weekday = b[i]-'A'+1;
            if(weekday==1) result += "MON ";
            else if(weekday==2) result += "TUE ";
            else if(weekday==3) result += "WED ";
            else if(weekday==4) result += "THU ";
            else if(weekday==5) result += "FRI ";
            else if(weekday==6) result += "SAT ";
            else if(weekday==7) result += "SUN ";
            cnt++;
        }
        else if(a[i]==b[i]&&(b[i]>='A'&&b[i]<='N'||b[i]>='0'&&b[i]<='9')&&cnt==1){
            int hour = 0;
            if(b[i]>='0'&&b[i]<='9') hour = b[i]-'0';
            else if(b[i]>='A'&&b[i]<='N') hour = b[i]-'A'+10;
            if(hour>=0&&hour<=9) result += "0" + to_string(hour);
            else result += to_string(hour);
            result += ":";
            break;
        }
    }
    minlen = min(c.length(), d.length());
    for(int i=0;i<minlen;i++){
        if(c[i]==d[i]&&(c[i]>='a'&&c[i]<='z'||c[i]>='A'&&c[i]<='Z')){
            if(i>=0&&i<=9) result += "0" + to_string(i);
            else result += to_string(i);
            break;
        }
    }
    cout<<result;
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值