PAT Basic 1029. 旧键盘

PAT Basic 1029. 旧键盘

1. 题目描述:

旧键盘上坏了几个键,于是在敲一段文字的时候,对应的字符就不会出现。现在给出应该输入的一段文字、以及实际被输入的文字,请你列出肯定坏掉的那些键。

2. 输入格式:

输入在 2 行中分别给出应该输入的文字、以及实际被输入的文字。每段文字是不超过 80 个字符的串,由字母 A-Z(包括大、小写)、数字 0-9、以及下划线 _(代表空格)组成。题目保证 2 个字符串均非空。

3. 输出格式:

按照发现顺序,在一行中输出坏掉的键。其中英文字母只输出大写,每个坏键只输出一次。题目保证至少有 1 个坏键。

4. 输入样例:

7_This_is_a_test
_hs_s_a_es

5. 输出样例:

7TI

6. 性能要求:

Code Size Limit
16 KB
Time Limit
200 ms
Memory Limit
64 MB

思路:

这里根据题目描述有文字只由字母,数字和下划线组成,并且最后输出字母时只输出大写,所以我当时定义了字符数组chCount用于按先后顺序统计输入文字中存在的字符,并且字母只统计大写字母。

然后就是根据chCount和输出文字去判断坏键,这里chCount保证了坏键只被输出一次。并且一个字母键如果坏掉,那么大小写字母是都不可能在输出中出现的。另外就是这里用到了库函数strchr()toupper(),tolower()

不知道当时怎么来的思路。。。现在回看真没想出来。

My Code:

#include <stdio.h>
#include <string.h>
#include <ctype.h>

// A-Z, 0-9, '_' sum up to 26+10+1 = 37 characters

int main(void)
{
    char in[81];
    char out[81];
    char chCount[38];
    int count = 0, i;
    char *pchar;
    
    scanf("%s%s", in, out);
    
//     printf("%s\n", in);
//     printf("%s\n", out);
    
    for(i=0; i<38; i++)
    {
        chCount[i] = '\0';
    }
    
    pchar = in;
    while(*pchar)
    {
        // char *strchr(const char *str, int c)
        if(strchr(chCount, toupper(*pchar)) == NULL)
        {
            chCount[count] = toupper(*pchar);
            count++;
        }
        pchar++;
    }
    
    //printf("%s\n", chCount);
    
    for(i=0; i<count; i++)
    {
        if(strchr(out, chCount[i]) == NULL && strchr(out, tolower(chCount[i])) == NULL)
        {
            printf("%c", chCount[i]);
        }
    }
    printf("\n");
    
    
    return 0;
}
posted @ 2023-03-16 20:15  十豆加日月  阅读(11)  评论(0编辑  收藏  举报