Pat1061 Dating

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

看到这道题我脑子里直接就跳出来了卷福的脸啊哈哈哈哈哈哈哈

易错点,
遍历完成第一个字符需要自增一下
遍历的字符需要符合时间范围的定义,不能出现字符匹配但是表示时间数溢出的情况

题目思路:(这个题目思路很简单)

  1. 前两个字串遍历第一个同样的大写字母,再转化为星期数存储
  2. 再继续遍历前两个字串,找符合要求的字母并转化为小时数
  3. 后两个字串遍历第一个相同的字符,转化为分钟数存储
#include <iostream>

using namespace std;

bool check(char c, int flag){	//flag区分三种不同情况的判断结果
    if(flag == 0){
        if( c >= 'A' && c <= 'G') return true;
        else 
            return false;
    }
    else if(flag == 1){
        if(c >= 'A' && c <= 'N' ||  c >= '0' && c <= '9') return true;
        else 
            return false;
    }else{
        if( c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z') return true;
        else
            return false;
    }
    
}

int to_hour(char c){
    if(c >= '0' && c <= '9') return c - '0';
    else    
        return c - 'A' + 10;
}

int main(){
    int d = -1, h = -1, m = -1, i = 0;
    int flag = 0;
    string s1,s2,s3,s4;
    getline(cin, s1);
    getline(cin, s2);
    getline(cin, s3);
    getline(cin, s4);
    
    string week[10] = {"MON","TUE","WED","THU","FRI","SAT","SUN"};
    
    while(i < s1.size() && i < s2.size()){
        if((s1[i] == s2[i] ) && check(s1[i], 0) && d == -1)
            d = s1[i++] - 'A';		//找到符合的之后预先自增一下
        if((s1[i] == s2[i] ) && check(s1[i], 1) && d != -1){
            h = to_hour(s1[i++]);	//找到符合的之后预先自增一下
            break;
        }
        i++;
    }
    
    i = 0;
    
    while(i < s3.size() && i < s4.size()){
        if(s3[i] == s4[i] && check(s3[i], 2)){
            m = i;
            break;
        }
        i++;
    }
    printf("%s %02d:%02d", week[d].c_str(), h, m);	//c风格字符输出需要添加一个字符指针
    
    return 0;
}

改良代码:

#include <cstdio>
#include <iostream>

using namespace std;

int main()
{
    string a, b, c, d;
    cin >> a >> b >> c >> d;

    int k = 0;
    while (true)	//简化了循环判断,首中即止
    {
        if (a[k] == b[k] && a[k] >= 'A' && a[k] <= 'G') break;	//在遍历中直接完成判断,判断函数不具有共用的特征,没必要写函数
        k ++ ;
    }

    char weekdays[7][4] = {"MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"};
    printf("%s ", weekdays[a[k] - 'A']);

    k ++ ;
    while (true)
    {
        if (a[k] == b[k] && (a[k] >= '0' && a[k] <= '9' || a[k] >= 'A' && a[k] <= 'N')) break;
        k ++ ;
    }

    printf("%02d:", a[k] <= '9' ? a[k] - '0' : a[k] - 'A' + 10);

    for (int i = 0;; i ++ )
        if (c[i] == d[i] && (c[i] >= 'a' && c[i] <= 'z' || c[i] >= 'A' && c[i] <= 'Z'))
        {
            printf("%02d\n", i);
            break;
        }

    return 0;
}

有些没什么功能重合的地方是没必要特地写个函数的,增加自己工作量,望引以为戒

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值