【PAT】1061. Dating (20)

题目链接:http://pat.zju.edu.cn/contests/pat-a-practise/1061

题目描述:


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)找出一二两个字符串中第一个相同的大写字母(注意是逐个比较,即对应的位置相同),注意这个字母的范围是‘A’ ~ ‘G’,所以不能简单用isupper来判断。
(3)找出一二两个字符串中第二个相同的大写字母或者数字。注意范围是’A‘~'N' 或者 '0' ~ '9'。 同样也不可以简单用isupper来判断。
(4)找出三四两个字符串中第一个相同的字母(不要求大写),记录下其出现的位置。

参考代码:


#include<iostream>
#include<string>
#include<string.h>
using namespace std;
<strong>//为什么定义为D[7][3]会报错???</strong>
char D[7][4] = {"MON","TUE","WED","THU","FRI","SAT","SUN"};

int main()
{
	char input[4][60];
	int i,j;
	bool find_first = false,find_second = false;
	char first,second;
	for(i=0; i<4; i++)
		cin>>input[i];
	char temp;

	for(i=0; i<strlen(input[0]) && i<strlen(input[1]) ; i++)
	{
		temp = input[1][i];
		if(input[0][i] == temp)
		{
			//第一个相同的大写字母范围为‘A’ ~ ‘G’;			 
			if(!find_first && (isupper(temp) && temp<='G') )
			{
				find_first = true;
				first = temp;
			}
			//第二个相同的大写字母范围为‘A’ ~ ‘N’;
			else if(find_first && !find_second && ( (isupper(temp)&&temp<='N') || isdigit(temp) ))
			{
				find_second = true;
				second = temp;
			}
			else if(find_first && find_second) 
					break;
		}		
	}	
	int date;
	char t;
	t = first;
	date = t - 'A';
	printf("%s ",D[date]);
	if(second > '9')
		cout<<second - 'A' + 10<<":";
	else 
		cout<<"0"<<second - '0'<<":";

	int position = -1;
	for(i=0; i<strlen(input[2]) && i<strlen(input[3]); i++)
	{
		if( input[2][i] == input[3][i] && isalpha(input[2][i]) )
		{
			position = i;
			break;
		}
	}
	if(position == -1) printf("00\n");
	else
	printf("%02d\n",position);	
	return 0;
}


今天重新写了下,更简洁些:
#include <iostream>
#include <string>
#include <vector>
#include <set>
using namespace std;

int main(int argc, char** argv) {
	string str1, str2, str3, str4;
	cin>>str1>>str2>>str3>>str4;
	int i;
	set<char> s;
	vector<char> vec1, vec2;
	char first='0', second='0';
	for(i=0; i<str1.size()&&i<str2.size(); i++){
		if(str1[i] == str2[i]){
			if(isupper(str1[i]) && str1[i]<='G' && first=='0'){
				first = str1[i];
			}else if(first!='0' && second=='0' && ((isupper(str1[i])&&str1[i]<='N')||isdigit(str1[i])) ){
				second = str1[i];
				break;
			}
		}
	}
	for(i=0; i<str3.size()&&i<str4.size(); i++){
		if(isalpha(str3[i]) && str3[i]==str4[i])
			break;
	}
	int m = i;
	char week[7][4] = {"MON","TUE","WED","THU","FRI","SAT","SUN"};
	int h = isalpha(second)? (second-'A'+10):(second-'0');
	printf("%s %02d:%02d\n",week[ first-'A'], h , m);	
	return 0;
}





你可能感兴趣的:(pat,cc++)