题目链接:请点击
思路:用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;
}