当前位置:编程学习 > C/C++ >>

PAT-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.
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;  
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;  
}  

 

 
补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,