当前位置: 代码迷 >> 综合 >> [PAT B1014/A1061]Dating
  详细解决方案

[PAT B1014/A1061]Dating

热度:83   发布时间:2023-12-15 06:23:34.0

[PAT B1014/A1061]Dating

本题是甲级1061和乙级1014题,所以先放英文题目,如果是要做乙级题的话,可以不看英文,直接看下面的中文题

题目描述

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.

输入格式

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.

输出格式

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.

输入样例

3485djDkxh4hhGE
2984akDfkkkkggEdsb
s&hgsfdk
d&Hyscvnm

输出样例

THU 14:04

--------------------------中文原题---------------------------

题目描述

1014 福尔摩斯的约会 (20 分)大侦探福尔摩斯接到一张奇怪的字条:我们约会吧! 3485djDkxh4hhGE 2984akDfkkkkggEdsb s&hgsfdk d&Hyscvnm。大侦探很快就明白了,字条上奇怪的乱码实际上就是约会的时间星期四 14:04,因为前面两字符串中第 1 对相同的大写英文字母(大小写有区分)是第 4 个字母 D,代表星期四;第 2 对相同的字符是 E ,那是第 5 个英文字母,代表一天里的第 14 个钟头(于是一天的 0 点到 23 点由数字 0 到 9、以及大写字母 A 到 N 表示);后面两字符串第 1 对相同的英文字母 s 出现在第 4 个位置(从 0 开始计数)上,代表第 4 分钟。现给定两对字符串,请帮助福尔摩斯解码得到约会的时间。

输入格式

输入在 4 行中分别给出 4 个非空、不包含空格、且长度不超过 60 的字符串。

输出格式

在一行中输出约会的时间,格式为 DAY HH:MM,其中 DAY 是某星期的 3 字符缩写,即 MON 表示星期一,TUE 表示星期二,WED 表示星期三,THU 表示星期四,FRI 表示星期五,SAT 表示星期六,SUN 表示星期日。题目输入保证每个测试存在唯一解。

解析

  1. 这道题目就是简单的模拟题,主要是要看明白题目,首先就是给出四个字符串,比较前两个字符串,找到第一个相同位置并且字符相同且在’A’和’G’之间的字母,就根据对应关系输出星期几,然后找到第二个相同位置,字符相同,并且在‘0’到‘9’或者在’A’到’N’之间的字母,根据对应输出小时数
  2. 然后比较后两个字符串,找到相同位置并且是英文字母的字符,它所在的位置就是分钟数,然后我们输出这个分钟数就可以了
#include<iostream>
#include<string>
using namespace std;
string week[8] = {
     "","MON","TUE","WED","THU","FRI","SAT","SUN" };//对应数组
int main()
{
    string s1, s2, s3, s4;cin >> s1 >> s2 >> s3 >> s4;    //四个字符串int i = 0, j = 0, k = 0;while (i<s1.length() && i<s2.length()) {
    if (s1[i] == s2[i] && s1[i]<='G' && s1[i]>='A') break;  //位置相同,且在A和G之间i++;}j = i + 1;while (j<s1.length() && j<s2.length()) {
    if ((s1[j] == s2[j])&&((s1[j] >= '0' && s1[j] <= '9')||(s1[j] >= 'A' && s1[j] <= 'N')))break;     //位置相同,且在0和9之间,或者在A和N之间j++;}cout << week[s1[i] - 'A' + 1] << " ";if (s1[j] >= '0' && s1[j] <= '9') printf("%02d", (int)(s1[j] - '0'));else printf("%02d", (int)(s1[j] - 'A' + 10));cout << ":";while (k < s3.length() && k < s4.length()) {
    if ((s3[k] == s4[k]) &&((s3[k] >= 'a' && s1[j] <= 'z')||((s3[k] >= 'A' && s1[j] <= 'Z'))))break;     //输出分钟数k++;}printf("%02d", k);return 0;
}

这里比较要注意的是这样写看上去很不清晰,所以我们可以使用isdigital()函数和isalpha()函数来判断究竟是不是数字或者英文字母,注意如果使用了这两个函数,一定一定要记得加入头文件cctype

水平有限,如果代码有任何问题或者有不明白的地方,欢迎在留言区评论;也欢迎各位提出宝贵的意见!