B1029 旧键盘 (20分)

打代码的时候对比了一下晴神和柳神的写法

发现晴神的代码里面对这题的解法是先把两个字符串中的小写字符都改成大写字符

接着进行比较,最后输出

而柳神则使用了字符串中的find函数,而find函数并没有区分大小写,也就是说在刚开始两个字符串比较的时候是区分大小写的??还是说???

这样会导致操作s1="A",而s2="B";的时候产生区别

我试了一下开始不转换的

 

注意:gets不可用,换成cin.getline(str,length);

晴神代码:

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include<algorithm> 
#include<map>
#include<vector>
#include<queue>  
#include<stack> 
#include<cctype>//里面有isdigit():数字字符返回true 
using namespace std; 


int main(){

char str1[100];
char str2[100]; 
bool HashTable[128]={false};//把字符转换为数字,而字符的范围最大是128 
cin.getline(str1,100);
cin.getline(str2,100);
int length1=strlen(str1);
int length2=strlen(str2);
 
for (int i=0;i<length1;i++){
	char c1;
	char c2;
	int j;
	for(j=0;j<length2;j++){
		c1=str1[i];
		c2=str2[j];
		if(c1>='a'&& c1<='z') c1-=32;//啊,有没有等号看清楚啊 
		if(c2>='a'&& c2<='z') c2-=32;
		//c1=toupper(c1);
		//c2=toupper(c2);
		if(c1==c2) break;
	}
	if(j==length2 && HashTable[c1]==false)//保证只输出一次
	{
		printf("%c",c1); 
		HashTable[c1]=true;
	}
	
	
}
	return 0;
}
 
 

柳神

#include<iostream>
#include<cctype>//toupper()
using namespace std; 


int main(){

string s1,s2,ans;
cin>>s1>>s2;
for(int i=0;i<s1.length();i++){
	if(s2.find(s1[i])==string::npos && ans.find(toupper(s1[i]))==string::npos){
		ans+=toupper(s1[i]);
	}
}
//toupper()转换小写字母到大写;转换成功,那么返回与 c 对应的大写字母;
//如果转换失败,那么直接返回 c(值未变)。
cout<<ans;
	return 0;
}
 
 

我:

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include<algorithm> 
#include<map>
#include<vector>
#include<queue>  
#include<stack> 
#include<cctype>//里面有isdigit():数字字符返回true 
using namespace std; 


int main(){

char str1[100];
char str2[100]; 
bool HashTable[128]={false};//把字符转换为数字,而字符的范围最大是128 
cin.getline(str1,100);
cin.getline(str2,100);
int length1=strlen(str1);
int length2=strlen(str2);
 
for (int i=0;i<length1;i++){
	char c1;
	char c2;
	int j;
	for(j=0;j<length2;j++){
		c1=str1[i];
		c2=str2[j];
		//if(c1>='a'&& c1<='z') c1-=32;//啊,有没有等号看清楚啊 
		//if(c2>='a'&& c2<='z') c2-=32;
		//c1=toupper(c1);
		//c2=toupper(c2);
		if(c1==c2) break;
	}
	if(j==length2 && HashTable[toupper(c1)]==false)//保证只输出一次
	{
		printf("%c",toupper(c1)); 
		HashTable[toupper(c1)]=true;
	}
	
	
}
	return 0;
}
 
 

这三个在PTA上都是对的!可能没有这个例子吧。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值