PAT 1029 旧键盘

1029 旧键盘(20 分)

旧键盘上坏了几个键,于是在敲一段文字的时候,对应的字符就不会出现。现在给出应该输入的一段文字、以及实际被输入的文字,请你列出肯定坏掉的那些键。

输入格式:

输入在 2 行中分别给出应该输入的文字、以及实际被输入的文字。每段文字是不超过 80 个字符的串,由字母 A-Z(包括大、小写)、数字 0-9、以及下划线 _(代表空格)组成。题目保证 2 个字符串均非空。

输出格式:

按照发现顺序,在一行中输出坏掉的键。其中英文字母只输出大写,每个坏键只输出一次。题目保证至少有 1 个坏键。

输入样例:

7_This_is_a_test
_hs_s_a_es

输出样例:

7TI

删除重复键思路:

将当前要对比的键temp,与已经存入result的进行对比,若存在(count++),则跳过该键,若不存在 (count=0)则存入result

#include<iostream>
#include<string>
#include<vector>
using namespace std;

int main()
{
	string a;
	string b;
	vector<char> c;
	cin >> a >> b;
	int n = 0;
	int len = a.length();
	for (int i = 0; i < len; i++)
	{
		if (a[n] != b[n])
		{
			if (a[n] > 'a'&&a[n] < 'z')
				a[n] = a[n] - 32;
			c.push_back(a[n]);//坏掉的键(有重复)
			a.erase(n, 1);
		}
		else
			n++;
	}
	//删除重复的键
	vector<char> result;
	char temp;
	int count = 0;
	for (int i = 0; i < c.size(); i++)
	{
		temp = c[i];
		for (int j = 0; j < result.size(); j++)
			if (temp == result[j])
			{
				count++;
				break;
			}
		if (count == 0)
			result.push_back(temp);
	}
	for (int i = 0; i < result.size(); i++)
		cout << result[i];
	system("pause");
	return 0;
}

 

 >=a  <=z

count=0

#include<iostream>
#include<string>
#include<vector>
using namespace std;

int main()
{
	string a;
	string b;
	vector<char> c;
	cin >> a >> b;
	int n = 0;
	int len = a.length();
	for (int i = 0; i < len; i++)
	{
		if (a[n] != b[n])
		{
			if (a[n] >= 'a'&&a[n] <= 'z')
				a[n] = a[n] - 32;
			c.push_back(a[n]);//坏掉的键(有重复)
			a.erase(n, 1);
		}
		else
			n++;
	}
	//删除重复的键
	vector<char> result;
	char temp;
	int count = 0;
	for (int i = 0; i < c.size(); i++)
	{
		count = 0;
		temp = c[i];
		for (int j = 0; j < result.size(); j++)
			if (temp == result[j])
			{
				count++;
				break;
			}
		if (count == 0)
			result.push_back(temp);
	}
	for (int i = 0; i < result.size(); i++)
		cout << result[i];
	system("pause");
	return 0;
}

 

 

改进:

string中的find() 函数,遍历s1字符 找该字符是否在s2中出现,未出现 存入ans

 还要注意!

如果ans中已经出现过s1中重复的字符,则不应该加入ans。

(找s1中的大写是否在ans中)

#include <iostream>
#include <string>
using namespace std;
int main()
{
	string s1, s2;
	cin >> s1 >> s2;
	string ans;
	for (int i = 0; i < s1.length(); i++)
	{
		//当前字符s1[i]不在s2中,
		//当前字符s1[i]的大写不在ans中,否则ans会重复
		if (s2.find(s1[i]) == string::npos && ans.find(toupper(s1[i])) == string::npos)
		{
			ans += toupper(s1[i]);
		}
	}
	cout << ans;
	system("pause");
	return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值