pat乙级 1033 旧键盘打字
旧键盘上坏了几个键,于是在敲一段文字的时候,对应的字符就不会出现。现在给出应该输入的一段文字、以及坏掉的那些键,打出的结果文字会是怎样?
输入格式:
输入在 2 行中分别给出坏掉的那些键、以及应该输入的文字。其中对应英文字母的坏键以大写给出;每段文字是不超过 10
5
个字符的串。可用的字符包括字母 [a-z, A-Z]、数字 0-9、以及下划线 _(代表空格)、,、.、-、+(代表上档键)。题目保证第 2 行输入的文字串非空。
注意:如果上档键坏掉了,那么大写的英文字母无法被打出。
输出格式:
在一行中输出能够被打出的结果文字。如果没有一个字符能被打出,则输出空行。
输入样例1:
7+IE.
7_This_is_a_test.
输出样例1:
_hs_s_a_tst
总结:
坑点:接收字符串时候需要用getline接收,因为第一行可能是空行。
分三种情况:
1需要输出的字符是否为大写字母
2.需要输出的字符是否为小写字母
3.其他
如果没一个输出,则输出空行
1如果为大写字母:
判断上档键是否坏了,如果坏了则不能输出。而且判断对应字母的键是否坏了,如果坏了则不能输出。 否则输出字母。
if (b[i] >= 'A'&&b[i] <= 'Z'){
if ( (nobig != 1) && (a.find(b[i])==a.npos) ){
cout << b[i];
noouput = 0;
}
}
2如果为小写字母:
判断对应的字母键是否坏了,因为用大写字母存储坏了的键。所以比对时要用小写字母-32
else if (b[i] >= 'a'&&b[i] <= 'z'){
if (a.find(b[i] - 32) == a.npos){
cout << b[i];
noouput = 0;
}
}
其他:
else
{
if (a.find(b[i]) == a.npos){
cout << b[i];
noouput = 0;
}
}
AC代码:
#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main(){
string a;
string b;
getline(cin,a);
getline(cin,b);
int nobig = 0;
int noouput = 1;
if (a.find('+') != a.npos){
nobig = 1;
}
for (int i = 0; i < b.size(); i++){
if (b[i] >= 'A'&&b[i] <= 'Z'){
if ( (nobig != 1) && (a.find(b[i])==a.npos) ){
cout << b[i];
noouput = 0;
}
}
else if (b[i] >= 'a'&&b[i] <= 'z'){
if (a.find(b[i] - 32) == a.npos){
cout << b[i];
noouput = 0;
}
}
else
{
if (a.find(b[i]) == a.npos){
cout << b[i];
noouput = 0;
}
}
}
if (noouput == 1){
cout << "";
}
system("pause");
return 0;
}