1. 程式人生 > >華為機試題C++-去除重複字元並排序

華為機試題C++-去除重複字元並排序

題目:去除重複字元並排序 

去除重複字元並排序

執行時間限制:無限制
內容限制:       無限制
輸入:              字串
輸出:              去除重複字元並排序的字串
樣例輸入:       aabcdefff

樣例輸出:       abcdef

C++實現如下:

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

void char_output(char *input)
{
       int hash[256];
	   memset(hash,0,sizeof(hash));
	   char *p=input;
	   while(*p!='\0')
	   {
	         hash[*p]++;
			 p++;
	   }
	   char *pbegin=input;
	   for(int i=0;i!=256;i++)
	   {
	         if(hash[i]!=0)
			 {
				 char ch=(char)i;
			      cout<<ch;
			 }
	   }
}

int main()
{
     //char ch[30]="ccaabbccghhy";
	 char ch[100];
	 gets_s(ch);
	 char_output(ch);
	 system("pause");
	 return 0;
}

注:可以用hash表直接實現,非常方便!而這其中涉及到一個強制轉換的問題(int轉char),輸出的字串就是排序的字串。