PTA甲级 1061 Dating

B1061 Dating (20分)

首先,先贴柳神的博客

https://www.liuchuo.net/ 这是地址

想要刷好PTA,强烈推荐柳神的博客,和算法笔记

题目原文

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

生词如下

capital 大写的

sensitive 敏感的

case

题目大意

就是要你解密,给定4个字符串要你解出对应的时间

开始的前两个字符串找天数和小时

最后的两个字符串找分钟

注意点:

① 天数有要求的:只能找A-G

② 小时也要要求,只能A-N或者0-9

③ 分钟只要是大写字母就可以的,但是分钟的计数是记录当前的位置,和数组一样的记录方式,0-59

④ 小时和分钟都一定有输出两位,不足的要补零

我自己垃圾代码如下:

#include<iostream>
#include<cstring>
#include<iomanip>
using namespace std;
int main(void) {
	int flag = 0, Position=1;
	char Put[4][61];
	char Day='0', Hour, Minute;
	for (int i = 0; i < 4; i++) {
		cin >> Put[i];
	}
	//Min选的是Put[0]和Put[1]里面最短的那个长度
	int Min = strlen(Put[0]) > strlen(Put[1]) ? strlen(Put[1]) : strlen(Put[0]);
	for (int i = 0; i < Min; i++) {
		if ((Put[0][i] >= 'A' && Put[0][i] <= 'N') || (Put[0][i] >= '0' && Put[0][i] <= '9')) {
			if (Put[0][i] == Put[1][i] && flag == 1) {
				Hour = Put[0][i];
				break;
			}
		}
		if (Put[0][i] >= 'A' && Put[0][i] <= 'G') {
			if (Put[0][i] == Put[1][i]&&flag==0) {
				Day = Put[0][i];
				flag = 1;
			}
		}
	}
	Min = strlen(Put[2]) > strlen(Put[3]) ? strlen(Put[3]) : strlen(Put[2]);
	for (int i = 0; i < Min; i++) {
		if ((Put[2][i] >= 'a' && Put[2][i] <= 'z') || (Put[2][i] >= 'A' && Put[2][i] <= 'Z')) {
			if (Put[2][i] == Put[3][i]) {
				Position = i;
				break;
			}
		}
	}

	if (Day == 'A')	cout << "MON ";
	else if (Day == 'B')cout << "TUE ";
	else if (Day == 'C')cout << "WED ";
	else if (Day == 'D')cout << "THU ";
	else if (Day == 'E')cout << "FRI ";
	else if (Day == 'F')cout << "SAT ";
	else if (Day == 'G')cout << "SUN ";
	else;
	
	if (Hour >= '0' && Hour <= '9') {
		cout << '0' << Hour<<':';
	}
	else if(Hour >= 'A' && Hour <= 'N') {
		cout << int(Hour - 55)<<':';
	}

	if (Position < 10&&Position>=0) {
		cout << '0' << Position;
	}
	else if(Position)
		cout << Position;
	
	return 0;
}

柳神的代码

#include <iostream>
#include <cctype>
using namespace std;
int main() {
    string a, b, c, d;
    cin >> a >> b >> c >> d;
    char t[2];
    int pos, i = 0, j = 0;
    while (i < a.length() && i < b.length()) {
        if (a[i] == b[i] && (a[i] >= 'A' && a[i] <= 'G')) {
            t[0] = a[i];
            break;
        }
        i++;
    }
    i = i + 1;
    while (i < a.length() && i < b.length()) {
        if (a[i] == b[i] && ((a[i] >= 'A' && a[i] <= 'N') || isdigit(a[i]))) {
            t[1] = a[i];
            break;
        }
        i++;
    }
    while (j < c.length() && j < d.length()) {
        if (c[j] == d[j] && isalpha(c[j])) {
            pos = j;
            break;
        }
        j++;
    }
    string week[7] = { "MON ", "TUE ", "WED ", "THU ", "FRI ", "SAT ", "SUN " };
    int m = isdigit(t[1]) ? t[1] - '0' : t[1] - 'A' + 10;
    cout << week[t[0] - 'A'];
    printf("%02d:%02d", m, pos);
    return 0;
}

总结和反思

柳神的代码比我的短一倍

主要是因为柳神用了

isdigit() 来判断是不是数字

isalpha() 来判断是不是字母

还有一个字符数组

我的就很蠢

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值