PAT 乙级 (Basic Level) Practice (中文)1029

1029 旧键盘 (20 分)

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

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

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

输入样例:

7_This_is_a_test
_hs_s_a_es

输出样例:

7TI

代码

#include <cstdio>
#include <cstring>
int main () {
	char a[100], b[100] ;
	bool hashTable[100] = {false} ;
	//hashTable[]=false 该字符在b中未出现 
	scanf ("%s", a) ;
	scanf ("%s", b) ;
	int len1=strlen(a), len2=strlen(b) ;
	for (int i=0; i<len1; i++) {
		for (int j=0; j<len2; j++) {
			if (a[i]>='a' && a[i]<='z') a[i] -= 32 ;
			if (b[j]>='a' && b[j]<='z') b[j] -= 32 ;
			//小写字母换成大写字母	 
			if (a[i] == b[j]) hashTable[a[i]]=true ;
			//出现该字符 
		}
	}
	for (int i=0; i<len1; i++) {
		if (hashTable[a[i]]==false) {
			printf ("%c", a[i]) ;
			hashTable[a[i]] = true ;
			//找到出现的字符就输出 并且置hashTable为true防止下一次遍历到相同的多次输出 
		}
	}
	return 0 ;
}

猜你喜欢

转载自blog.csdn.net/arthur01p/article/details/86655443