1084 Broken Keyboard

题意:给出两个字符串(有字母,数字和下划线_组成),第一个字符串str1是由键盘输入的字符串,第二个字符串str2是屏幕显示的字符串,问键盘有哪几个按键坏了,根据输入的顺序,输出坏掉的键(仅输出一次)。要求输出字母为大写。

 思路:
1、因为不区分大小写且要求输出为大写,因此,对输入的字符均先统一转换成大写,可以用toupper(char ch)函数,在头文件<ctype.h>下面
2、定义bool goodKeys[128],初始化为false。先遍历str2,标记出现过的字符为true;然后遍历str1,若某个字符为false,说明这个字符没有出现过,也就是坏掉了,于是把这个字符输出,因为坏掉的键只需要输出一次即可,因此输出一次后就立即标记为true。

代码:

#include <cstdio>
#include <cstring>
#include <ctype.h>

int main()
{
    char str1[100], str2[100];
    bool goodKeys[128]={false};//goodKeys[ch]为true表示字符ch为完好的键
    scanf("%s",str1);
    scanf("%s",str2);
    int len=strlen(str2);
    for(int i=0;i<len;i++){
        str2[i]=toupper(str2[i]);//小写转大写
        goodKeys[str2[i]]=true;
    }    
    len=strlen(str1);
    for(int i=0;i<len;i++){
        str1[i]=toupper(str1[i]);
        if(goodKeys[str1[i]]==false) {
            goodKeys[str1[i]]=true;
            printf("%c",str1[i]);
        }
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/kkmjy/p/9551676.html