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;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章