Dating (20)

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

大意
给出两对字符串,按照要求分析这两对字符串得到3个字母,再按照这三个字母以及给出的规则转换成一个时间。

分析
主要考察字符串处理,然后就是从题目要求正确梳理出要找什么字符,怎样正确找,以及怎样正确处理字符。不难,但是如果不是给出错在哪里的话,可能很难想到是代码逻辑错了还是理解题目错了。

MY CODE

#include<iostream>
#include<string>
#include<string.h>
#define jdg1(t) ( (t >= 'a' && t<='z') || ( t>='A' && t <= 'Z'))
#define jdg2(t) ((t >= 'a' && t<='z') || (t>='A' && t <= 'Z') || (t>='0' && t<= '9'))
using namespace std;

void PrintResutlt(char a, char b, int c){
	string day[] = { "MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN" };
	cout << day[a - 'A'];
	int min = ('0' <= b && b <= '9' ? b - '0' : 10+b - 'A');
	printf(" %02d:%02d\n", min, c);
}

int main(){
	string a1, a2, b1, b2;
	cin >> a1 >> a2 >> b1 >> b2;
	int index = 0;
	char c1=' ', c2=' ';
	int i3 = 0, i=0;
	for (; !jdg1(a1[i]) || !jdg1(a2[i]) || a1[i] != a2[i]; i++);
	c1 = a1[i];
	for (i++; !jdg2(a2[i]) || !jdg2(a2[i]) || a1[i] != a2[i]; i++);
	c2 = a1[i];
	for (i = 0; !jdg1(b1[i]) || !jdg2(b2[i]) || b1[i] != b2[i]; i++){
		if (!jdg1(b1[i]) || !jdg2(b2[i])) index--;
	}
	index += i + 1;
	PrintResutlt(c1, c2, index);
	//cout << c1 << "  " << "  " << c2 << "   " << index << endl;
}

随笔
题目中说道:the first common capital English letter…以及the second common character is…这里需要区分开来,找到第一个和第二个字符的判断条件不同,第一个字符中能是字母,第二个字符可以是数字。
在第二次提交出错,是因为判断条件中因为不够仔细,忽略了‘等于’的情况。

发布了90 篇原创文章 · 获赞 31 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/BlackCarDriver/article/details/95449293