字符串排序/华为机试(C/C++)

版权声明:本文为博主原创文章,转载请注明出处 https://blog.csdn.net/thecentry。 https://blog.csdn.net/thecentry/article/details/82463688

题目描述

编写一个程序,将输入字符串中的字符按如下规则排序。

规则 1 :英文字母从 A 到 Z 排列,不区分大小写。

       如,输入: Type   输出: epTy

规则 2 :同一个英文字母的大小写同时存在时,按照输入顺序排列。

     如,输入: BabA   输出: aABb

规则 3 :非英文字母的其它字符保持原来的位置。

     如,输入: By?e   输出: Be?y

样例:

    输入:

   A Famous Saying: Much Ado About Nothing(2012/8).

    输出:

   A  aaAAbc   dFgghh :  iimM   nNn   oooos   Sttuuuy  (2012/8).

输入描述:

输出描述:

示例1

输入

A Famous Saying: Much Ado About Nothing (2012/8).

输出

A aaAAbc dFgghh: iimM nNn oooos Sttuuuy (2012/8).

代码:只是改了一下别人的代码

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>

using namespace std;

int main()
{
	string str;
	char ch;
	while (getline(cin, str))
	{
		vector<char> vc;
		int n = str.length();
		for (int i = 0; i<26; i++)
		{
			for (int j = 0; j<n; j++)
			{
				if (str[j] - 'A' == i || str[j] - 'a' == i)
				{
					vc.push_back(str[j]);
				}
			}
		}
		int k=0;
		for (int i = 0; i<str.length() && k<vc.size(); i++)
		{
			if (isalpha(str[i]))
				str[i] = vc[k++];
		}
		cout << str.c_str()<<endl;
	}
	return 0;
}

代码2:自己写的有一个bug

//第二十六题 字符串排序
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
bool MyFunction(char a, char b)
{
	if (isupper(a) && isupper(b))
	{
		return (a < b);
	}
	else if (islower(a) && islower(b))
	{
		return (a < b);
	}
	else if (islower(a) && isupper(b))
	{
		char c = a - 32;
		return ((c) < b);
	}
	else if (isupper(a) && islower(b))
	{
		char c = b - 32;
		return (a < (c));
	}
	else
		return (a<b);
}
int main()
{
	string str;
	while (getline(cin,str))
	{
		vector<int>vPosition;
		string sDel;
		size_t iMax = str.length();
		for (int i = 0; i < iMax; i++)
		{
			if (isalpha(str[i]))
			{
				sDel += str[i];
			}
			else
			{
				vPosition.push_back(i);
			}
		}
		cout << sDel.c_str() << endl;
		sort(sDel.begin(), sDel.end(), MyFunction);
		iMax = vPosition.size();
		for (int i = 0; i < iMax; i++)
		{
			int tPosition = vPosition[i];
			sDel.insert(tPosition, 1, str[tPosition]);
		}
		cout << sDel.c_str() << endl;
	}
	system("pause");
	return 0;
}

代码3:发现用sort函数中排序达不到我写的函数的效果,冒泡排序符合我的逻辑

//第二十六题 字符串排序
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<string.h>
using namespace std;
bool MyFunction(char a, char b);
void swap(char* a, char* b)
{
	char temp;
	temp = *a;
	*a = *b;
	*b = temp;
}
void bubble(char a[], int n)
{
	for (int i = 0; i < n - 1; i++)
	{
		if (MyFunction(a[i],a[i+1]))
			swap(a[i], a[i + 1]);
	}
}
string bubbleSort(string b, int n)
{
	char* tb = new char[n+1];
	memcpy(tb, b.data(), n);
	tb[n] = '\0';
	for (int i = n; i > 1; i--)
	{
		bubble(tb, i);
	}
	string b2 = tb;
	delete[] tb;
	return b2;
}

bool MyFunction(char a, char b)
{
	if (isupper(a) && isupper(b))
	{
		return (a > b);
	}
	else if (islower(a) && islower(b))
	{
		return (a > b);
	}
	else if (islower(a) && isupper(b))
	{
		char c = a - 32;
		return ((c) > b);
	}
	else if (isupper(a) && islower(b))
	{
		char c = b - 32;
		return (a > (c));
	}
	else
		return (a<b);
}
int main()
{
	string str;
	while (getline(cin,str))
	{
		vector<int>vPosition;
		string sDel;
		size_t iMax = str.length();
		for (int i = 0; i < iMax; i++)
		{
			if (isalpha(str[i]))
			{
				sDel += str[i];
			}
			else
			{
				vPosition.push_back(i);
			}
		}
		sDel = bubbleSort(sDel.data(), sDel.length());
		iMax = vPosition.size();
		for (int i = 0; i < iMax; i++)
		{
			int tPosition = vPosition[i];
			sDel.insert(tPosition, 1, str[tPosition]);
		}
		cout << sDel.c_str() << endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/thecentry/article/details/82463688