PAT B1033 旧键盘打字
旧键盘上坏了几个键,于是在敲一段文字的时候,对应的字符就不会出现。现在给出应该输入的一段文字、以及坏掉的那些键,打出的结果文字会是怎样?
输入格式:
输入在 2 行中分别给出坏掉的那些键、以及应该输入的文字。其中对应英文字母的坏键以大写给出;每段文字是不超过 1 个字符的串。可用的字符包括字母 [a-z, A-Z]、数字 0-9、以及下划线 _(代表空格)、, 、 . 、 - 、+(代表上档键)。题目保证第 2 行输入的文字串非空。
注意:如果上档键坏掉了,那么大写的英文字母无法被打出。
输出格式:
在一行中输出能够被打出的结果文字。如果没有一个字符能被打出,则输出空行。
输入样例:
7+IE. 7_This_is_a_test.
输出样例:
_hs_s_a_tst
这道题应该是我比较早做的PAT的题目,刚开始自己做这道题的时候输入两个字符串的时候,利用字符数组来做,我开始想的是使用的是gets(),但是编译并不通过,于是查找之后,发现PAT不让使用gets(),所以之后我就用了cin.getline(str,maxn),发现这样始终是通不过最后一个点,苦恼了很久之后,将cin.getline()改为了用getline()(它string库函数下),改为利用string字符串来做,就全通过了.
始终是没弄明白为什么,麻烦懂的告诉我
下面是两种通过与否的代码:
若利用字符数组和cin.getline()(最后一个点未通过)
#include<stdio.h> #include<iostream> #include<cstring> #include <ctype.h> using namespace std; const int maxn = 10010; char str1[maxn]; char str2[maxn]; bool hashtable[maxn]; int main() { memset(hashtable, true, sizeof(hashtable)); cin.getline(str1, maxn); cin.getline(str2, maxn); int len1 = strlen(str1); int len2 = strlen(str2); for (int i = 0; i < len1; i++) { hashtable[str1[i]] = false; } for (int i = 0; i < len2; i++) { if (str2[i] >= 'A' && str2[i] <= 'Z') { if ((hashtable[str2[i]] == true) && hashtable['+'] == true) { printf("%c", str2[i]); } } else if (str2[i] >= 'a' && str2[i] <= 'z') { int temp = toupper(str2[i]); if (hashtable[temp] == true) { printf("%c", str2[i]); } } else { if (hashtable[str2[i]] == true) { printf("%c", str2[i]); } } } printf("\n"); return 0; }
若利用string和getline():
#include<cstdio> #include<iostream> #include<cstring> #include<string> #include <ctype.h> const int maxn = 10010; using namespace std; string str1; string str2; bool hashtable[maxn]; int main() { memset(hashtable, true, sizeof(hashtable)); getline(cin, str1); getline(cin, str2); int len1 = str1.length(); int len2 = str2.length(); for (int i = 0; i < len1; i++) { hashtable[str1[i]] = false; } for (int i = 0; i < len2; i++) { if (str2[i] >= 'A' && str2[i] <= 'Z') { if ((hashtable[str2[i]] == true) && hashtable['+'] == true) { printf("%c", str2[i]); } } else if (str2[i] >= 'a' && str2[i] <= 'z') { int temp = toupper(str2[i]); if (hashtable[temp] == true) { printf("%c", str2[i]); } } else { if (hashtable[str2[i]] == true) { printf("%c", str2[i]); } } } printf("\n"); return 0; }
现附上柳婼姐姐的代码:
#include <iostream> #include <cctype> using namespace std; int main() { string bad, should; getline(cin, bad); getline(cin, should); for (int i = 0, length = should.length(); i < length; i++) { if (bad.find(toupper(should[i])) != string::npos) continue; if (isupper(should[i]) && bad.find('+') != string::npos) continue; cout << should[i]; } return 0; }

浙公网安备 33010602011771号