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() 来判断是不是字母

还有一个字符数组

我的就很蠢

PTA甲级B1061 Dating的更多相关文章

  1. PTA甲级1094 The Largest Generation (25分)

    PTA甲级1094 The Largest Generation (25分) A family hierarchy is usually presented by a pedigree tree wh ...

  2. PAT 甲级 1061 Dating (20 分)(位置也要相同,题目看不懂)

    1061 Dating (20 分)   Sherlock Holmes received a note with some strange strings: Let's date! 3485djDk ...

  3. PAT甲级——A1061 Dating

    Sherlock Holmes received a note with some strange strings: Let's date! 3485djDkxh4hhGE 2984akDfkkkkg ...

  4. PAT甲级——1061 Dating (20分)

    Sherlock Holmes received a note with some strange strings: Let's date! 3485djDkxh4hhGE 2984akDfkkkkg ...

  5. PAT甲级——1061 Dating

    1061 Dating Sherlock Holmes received a note with some strange strings: Let's date! 3485djDkxh4hhGE 2 ...

  6. PAT甲级1061 Dating

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805411985604608 题意: 给定四个字符串. 前两个字符串 ...

  7. PTA 甲级 1139

    https://pintia.cn/problem-sets/994805342720868352/problems/994805344776077312 其实这道题目不难,但是有很多坑点! 首先数据 ...

  8. PTA甲级—链表

    1032 Sharing (25分) 回顾了下链表的基本使用,这题就是判断两个链表是否有交叉点. 我最开始的做法就是用cnt[]记录每个节点的入度,发现入度为2的节点即为答案.后来发现这里忽略了两个链 ...

  9. PTA甲级—STL使用

    1051 Pop Sequence (25分) [stack] 简答的栈模拟题,只要把过程想清楚就能做出来. 扫描到某个元素时候,假如比栈顶元素还大,说明包括其本身的在内的数字都应该入栈.将栈顶元素和 ...

随机推荐

  1. 如何在国内下载Eclipse及其插件

    北京理工大学 http://mirror.bit.edu.cn/eclipse/ 中国科学技术大学 http://mirrors.ustc.edu.cn/eclipse/ 大连东软信息学院 http: ...

  2. vue初始化、数据处理、组件传参、路由传参、全局定义CSS与JS、组件生命周期

    目录 项目初始化 组件数据局部化处理 子组件 父组件 路由逻辑跳转 案例 组件传参 父传子 子组件 父组件 子传父 子组件 父组件 组件的生命周期钩子 路由传参 第一种 配置:router/index ...

  3. 机器学习总结-bias–variance tradeoff

    bias–variance tradeoff 通过机器学习,我们可以从历史数据学到一个\(f\),使得对新的数据\(x\),可以利用学到的\(f\)得到输出值\(f(x)\).设我们不知道的真实的\( ...

  4. ELK:收集Docker容器日志

    简介 之前写过一篇博客 ELK:日志收集分析平台,介绍了在Centos7系统上部署配置使用ELK的方法,随着容器化时代的到来,容器化部署成为一种很方便的部署方式,收集容器日志也成为刚需.本篇文档从 容 ...

  5. 技术|Android安装包优化

    版权声明 1.本文版权归原作者所有,转载需注明作者信息及原文出处. 2.本文作者:赵裕(vimerzhao),永久链接:https://github.com/vimerzhao/vimerzhao.g ...

  6. Spring事务中的事务传播行为

    1.支持当前事务: TransactionDefinition.PROPAGATION_REQUIRED:如果当前存在事务,则加入该事务:如果当前没有事务,则创建一个新的事务. Transaction ...

  7. 并发编程之Master-Worker模式

    我们知道,单个线程计算是串行的,只有等上一个任务结束之后,才能执行下一个任务,所以执行效率是比较低的. 那么,如果用多线程执行任务,就可以在单位时间内执行更多的任务,而Master-Worker就是多 ...

  8. [redis读书笔记] 第一部分 数据结构与对象 压缩列表

    压缩列表是为了节省内存而设计的,是列表键和哈希键的底层实现之一. 压缩列表的逻辑如下,

  9. debian wget 报ERROR: The certificate of ‘xxx’ is not trusted.

    # wget https://www.python.org/ftp/python/3.6.8/Python-3.6.8.tar.xz --2019-01-11 15:04:41-- https://w ...

  10. OptaPlanner 7.32.0.Final版本彩蛋 - SolverManager之批量求解

    上一篇介绍了OptaPlanner 7.32.0.Final版本中的SolverManager接口可以实现异步求解功能.本篇将继续介绍SolverManager的另一大特性 - 批量求解. 适用场景 ...