PAT乙级1033

题目链接

https://pintia.cn/problem-sets/994805260223102976/problems/994805288530460672

题解

刚开始没有理解到,如果上档键坏的话,所有大写字母都不行(My bad),后来改代码思路就很乱,甚至还考虑过_+是否输出之类的……所以理解题意很重要吧。这个题弄得我脑袋很累。

考虑到上档键坏的话,所有大写字母都不行之后,还是会有一个测试点不过。

另外有个坑是第一行可能是空行,即所有键都是可以使用的。

// PAT BasicLevel 1033
// https://pintia.cn/problem-sets/994805260223102976/problems/994805288530460672

#include <iostream>
#include <string>
using namespace std;

int main()
{
    // 用户输入的字符串、输出的字符串、坏掉的键(字符串形式)
    string strIn,wrongKeys;
    getline(cin, wrongKeys);
    cin >> strIn;

    // 上档键(大写)是否可用
    bool upIsWrong = (wrongKeys.find('+') == string::npos) ? false : true;

    // 生成输出字符串
    for(int i=0;i<strIn.length();++i){
        
        // 处理坏掉的键导致对应键无法输出
        if (wrongKeys.find(toupper(strIn[i]))!=string::npos){
            continue;
        }
        // 处理上档键坏掉导致大写无法输出
        if (isupper(strIn[i]) && upIsWrong){
            continue;
        }

        // 这个键没有问题
        cout << strIn[i];
    }
    
    //system("pause");
    return 0;
}

作者:@臭咸鱼

转载请注明出处:https://www.cnblogs.com/chouxianyu/

欢迎讨论和交流!


原文地址:https://www.cnblogs.com/chouxianyu/p/11324707.html