PAT算法笔记(五)————旧键盘*

来源:互联网 发布:c语言打爱心 编辑:程序博客网 时间:2024/05/29 10:41

题目描述

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

输入描述:

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


输出描述:

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

输入例子:

7_This_is_a_test_hs_s_a_es

输出例子:

7TI

代码:

#include "stdio.h"


char str1[81];
char str2[81];
char badKeySet[80];


static int BadKeyCursor = 0;
void JoinInBadKeys(char bad)
{
if (bad >= 'a' && bad <= 'z')
bad -= 0x20;
for (int i = 0; i < BadKeyCursor; i++)
{
if (badKeySet[i] == bad)
return;
}
badKeySet[BadKeyCursor++] = bad;
}
int main()
{
gets(str1);
gets(str2);
int pos1 = 0, pos2 = 0;

//判断条件十分重要,考虑str1后半段全为坏掉的字符的情况
while(str2[pos2] != '\0' || str1[pos1] != '\0')
{
if (str1[pos1] != str2[pos2])
{
JoinInBadKeys(str1[pos1]);
pos1++;
}
else
{
pos1++;
pos2++;
}
}
for (int i = 0; i < BadKeyCursor; i++)
printf("%c", badKeySet[i]);
return 0;
}

重点:

考虑情况不足,注释中为容易犯的错误。

0 0
原创粉丝点击