PAT-乙-1029 1029 旧键盘 (20 分)

在这里插入图片描述

代码

#include <iostream>

using namespace std;

int main(){
	
	string s1, s2;
	cin>>s1>>s2;
	
	//equals to java: toUpperCase()
	for(int i=0; i<s1.length(); i++){
		if(s1.at(i)>='a' && s1.at(i)<='z'){
			s1[i] = s1[i]-32;
		}
	}
	for(int i=0; i<s2.length(); i++){
		if(s2.at(i)>='a' && s2.at(i)<='z'){
			s2[i] = s2[i]-32;
		}
	}
	
	char ch[129] = {0};
	for(int i=0; i<s2.length(); i++){
		if(ch[s2.at(i)]==0){
			ch[s2.at(i)] = 1;
		}
	}
	
	for(int i=0; i<s1.length(); i++){
		if(ch[s1.at(i)]==0){
			//guarantee one letter only output once
			ch[s1.at(i)] = -1;
			cout<<s1.at(i);
		}
	}
	cout<<endl;
	
	return 0;
} 

注解

由于只输出大写,因此首先应转换成大写。然后利用Hash的思想,找哪些是坏键。如何每个坏键只输出一次,且按照输入顺序依次输出?解决方法是输出后赋值-1,避免再次输出。遍历时按照输入顺序遍历。

结果

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/zhanggirlzhangboy/article/details/82941249
今日推荐