【再思考】PATBasic——1033. 旧键盘打字(20)

来源:互联网 发布:c数值算法 编辑:程序博客网 时间:2024/05/18 01:28

1033. 旧键盘打字(20)

时间限制
200 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Yue

旧键盘上坏了几个键,于是在敲一段文字的时候,对应的字符就不会出现。现在给出应该输入的一段文字、以及坏掉的那些键,打出的结果文字会是怎样?

输入格式:

输入在2行中分别给出坏掉的那些键、以及应该输入的文字。其中对应英文字母的坏键以大写给出;每段文字是不超过105个字符的串。可用的字符包括字母[a-z, A-Z]、数字0-9、以及下划线“_”(代表空格)、“,”、“.”、“-”、“+”(代表上档键)。题目保证第2行输入的文字串非空。

注意:如果上档键坏掉了,那么大写的英文字母无法被打出。

输出格式:

在一行中输出能够被打出的结果文字。如果没有一个字符能被打出,则输出空行。

输入样例:
7+IE.7_This_is_a_test.
输出样例:

_hs_s_a_tst

#include<vector>#include <sstream>#include<cmath>#include<iomanip>#include<iostream>#include <ctype.h>#include <stdlib.h>using namespace std;int main(){string a, b;cin >> a >> b;//a = "7IE.";//b = "7_This_is_a_test.";int ifcap= a.find('+');vector<char> outputs;if (ifcap >= 0)//如果字符串中包含加号{for (int i = 0; i < b.length(); i++){char bi = b[i];if (!isupper(bi))//如果不是大写字母,进行以下操作{int flag = 0;for (int j = 0; j < a.length(); j++){if (bi == a[j]){break;}else if ((isalpha(bi)) && (abs(bi - a[j]) == 32))//判断是否为字母,且不区分大小写{break;}else//注意这里不能直接push_back,应该是与a中字符都对应判断过了,没有出现,再放入容器中{flag++;}}if (flag == a.size()){outputs.push_back(bi);}}else//如果是大写字母,直接跳到下一字符{continue;}}}else//如果不包含加号{for (int i = 0; i < b.length(); i++){char bi = b[i];int flag = 0;for (int j = 0; j < a.length(); j++){if (bi == a[j]){break;}else if ((isalpha(bi)) && (abs(bi - a[j]) == 32)){break;}else{flag++;}}if (flag == a.size()){outputs.push_back(bi);}}}if (outputs.size() == 0){cout << endl;}else{for (int i = 0; i < outputs.size(); i++){cout << outputs[i];}}return 0;}

第二版本,得分20分。回头想想,这题真的不难,居然还能把我纠结这么久!总结经验:
1、读题,理解清楚题意很重要!每个题拿到后,认真读题,分析,在纸上写下思路,然后才开始写代码;
2、用if-else的时候,要注意,考虑了if,就要对应到else,不要想当然的以为是自己考虑的那些情况,其实很多时候,都会漏掉边界条件
#include<vector>#include <sstream>#include<cmath>#include<iomanip>#include<iostream>#include <ctype.h>#include <stdlib.h>#include <algorithm>using namespace std;bool IfinAstr(char c, string a)//函数判断字符c是否在字符串a中,返回bool型的变量{bool temp = false;for (int i = 0; i < a.length(); i++){if (c == a[i]){temp = true;}}return temp;}int main(){string a, b;getline(cin, a);getline(cin, b);if (a == "")//一定要注意这里,a有可能是空{cout << b;}else{bool ifupper = false;//默认大写键是好的。可以输出大写字母int num = a.find('+');if (num >= 0){ifupper = true;}for (int i = 0; i < b.length(); i++){char c = b[i];if (isalpha(c))//如果是字母{if (isupper(c))//如果是大写字母{if ((ifupper == false) && (!IfinAstr(c, a)))//如果上档键没坏,且该字符不在a里{cout << c;}else{continue;}}else//如果是小写字母{if (!IfinAstr(toupper(c), a))//判断其对应的大写字母是否在其中{cout << c;}else{continue;}}}else//如果不是字母{if (!IfinAstr(c, a)){cout << c;}else{continue;}}}}return 0;}


0 0