PAT乙级—1033 旧键盘打字 (20分)

旧键盘上坏了几个键,于是在敲一段文字的时候,对应的字符就不会出现。现在给出应该输入的一段文字、以及坏掉的那些键,打出的结果文字会是怎样?
输入格式:
输入在 2 行中分别给出坏掉的那些键、以及应该输入的文字。其中对应英文字母的坏键以大写给出;每段文字是不超过 10​5​​ 个字符的串。可用的字符包括字母 [a-z, A-Z]、数字 0-9、以及下划线 _(代表空格)、,、.、-、+(代表上档键)。题目保证第 2 行输入的文字串非空。
注意:如果上档键坏掉了,那么大写的英文字母无法被打出。
输出格式:
在一行中输出能够被打出的结果文字。如果没有一个字符能被打出,则输出空行。

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

思路:
  先判断是否能大写,不能大写的话,直接跳过所有大写,然后再双重循环比较

注意:

  1. 注意审题,上档键只有一个‘+’
  2. 注意输入,由于第一行可以为空,故不能直接用cin来输入,得读取一行,用getline(cin,str)

代码:(C++)

#include<iostream>

using namespace std;

int main()
{
    string brake_key,input;
    
//  cin>>brake_key>>input;
    getline(cin,brake_key);
    getline(cin,input);
    bool is_up = true;
    
    for(int i=0; i<brake_key.length(); i++)
    {
        if(brake_key[i] == '+')
        {
            is_up = false;
            break;
        }
    }
    
    for(int i=0; i<input.length(); i++)
    {
        if(input[i] >='A' && input[i] <='Z' && !is_up)
        {
            continue;
        }
        if(input[i] >='a' && input[i] <='z')
        {
            bool is_case = false;
            for(int j=0; j<brake_key.length(); j++)
            {
                if((input[i]-32) == brake_key[j])
                {
                    is_case = true;
                    break;
                }
            }
            if(is_case)
                continue;
            cout<<input[i];
        }
        else
        {
            bool is_case = false;
            for(int j=0; j<brake_key.length(); j++)
            {
                if((input[i]) == brake_key[j])
                {
                    is_case = true;
                    break;
                }
            }
            if(is_case)
                continue;
            cout<<input[i];
        }
        
    }
    
    return 0;
}

可以直接利用C++的内置函数:不用自己双重循环了

  1. toupper() 变大写,有返回值
  2. find() 搜索内容
#include <iostream>
#include <cstdlib>
using namespace std;
int main(){
    string a,b;
    getline(cin,a);
    getline(cin,b);
    bool containPlus=false;
    for(int i=0;i<a.length();i++){
        if(a[i]=='+'){
            containPlus=true;
            continue;
        }
        a[i]=toupper(a[i]);
    }
    for(int i=0;i<b.length();i++){
        if(containPlus&&b[i]>='A'&&b[i]<='Z')continue;
        if(a.find(toupper(b[i]))==a.npos) cout<<b[i];
    }
    system("pause");
    return 0;
}
发布了77 篇原创文章 · 获赞 20 · 访问量 5797

猜你喜欢

转载自blog.csdn.net/qq_42396168/article/details/104950179
今日推荐