PAT 1033 旧键盘打字

题目链接:请点击
思路:用string定义两个字符串,然后比较就可以了。然而,开始直接用cin>>str1>>str2,导致有部分测试点始终未过去,后来参考他人的博客才发现这里应该用getline原因就在于第一行可能是空行。错误代码

#include<iostream>
#include<cctype>
#include<string>
using namespace std;
int main()
{
    string str1,str2;
    int flag=0;
    cin>>str1>>str2;
    if(str1.find('+')!=string::npos)
        flag=1;
    for(int i=0;i<str1.length();i++)
    {
        if(isupper(str1[i]))
            str1[i]=str1[i]+('a'-'A');
    }
    for(int i=0;i<str2.length();i++)
    {
        if(isupper(str2[i])&&flag)
            continue;
        if(str1.find(str2[i])!=string::npos)
            continue;
        cout<<str2[i];
    }
    return 0;
}

随后更改代码,但是还是一直会有最后一个测试点未过去,也非常纳闷

#include<iostream>
#include<cctype>
#include<string>
using namespace std;
int main()
{
    string str1,str2;
    int flag=0;
    getline(cin,str1);
    getline(cin,str2);
    if(str1.find('+')!=string::npos)
        flag=1;
    for(int i=0;i<str1.length();i++)
    {
        if(isupper(str1[i]))
            str1[i]=tolower(str1[i]);
    }
    for(int i=0;i<str2.length();i++)
    {
        if(isupper(str2[i])&&flag)
            continue;
        if(str1.find(str2[i])!=string::npos)
            continue;
        cout<<str2[i];
    }
    return 0;
}

这个最后测试点未过去的原因,一直没有找到,也是参考他人,但是也是没对,后来无奈就瞎整,结果还碰巧好了。但是一直很有疑问,我把str1的大写换成小写就未通过,怎么把str2中的小写换成大写来检验在str1中是否存在就对了???

#include<iostream>
#include<cctype>
#include<string>
using namespace std;
int main()
{
    string str1,str2;
    int flag=0;
    getline(cin,str1);//为防止第一行是空行用getline
    getline(cin,str2);
    if(str1.find('+')!=string::npos)
        flag=1;
    for(int i=0;i<str2.length();i++)
    {
        if(isupper(str2[i])&&flag)
            continue;
        if(str1.find(toupper(str2[i]))!=string::npos)
            continue;
        cout<<str2[i];
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值