码迷,mamicode.com
首页 > 编程语言 > 详细

去除重复字符并排序

时间:2016-03-27 12:22:43      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:

这里有两种方法,一个是把输入的一串字符进行排序,然后输出不重复的字符;
二是用一个长度为256的数组进行标记,有点像hash表的方式!!

1
#include <iostream> 2 3 #include <string> 4 5 using namespace std; 6 7 void sorthash(string ptr ,int length) 8 { 9 char hash[256] = { 0 }; 10 int i = 0; 11 for (i = 0; i < length; i++) 12 { 13 if (0 == hash[ptr[i]]) 14 { 15 hash[ptr[i]] = 1; 16 } 17 } 18 for (i = 0; i < 256; i++) 19 { 20 if (hash[i] == 1) 21 { 22 cout << (char)i; 23 } 24 25 } 26 27 } 28 void sort(string str1,int length)//冒泡 29 { 30 char temp; 31 for (int j = 0; j < length - 1; j++) 32 { 33 for (int i = 0; i < length - 1 - j; i++) 34 { 35 if (str1[i]>str1[i + 1]) 36 { 37 temp = str1[i]; 38 str1[i] = str1[i + 1]; 39 str1[i + 1] = temp; 40 } 41 } 42 } 43 cout << str1[0]; 44 for (int i = 0; i < length - 1; i++) 45 { 46 47 if (str1[i + 1] != str1[i]) 48 { 49 cout<<str1[i + 1]; 50 } 51 } 52 } 53 54 55 int main(void) 56 { 57 string str = " "; 58 int len = 0; 59 60 cin >> str; 61 len = str.length(); 62 sort(str,len); 63 //sorthash(str, len); 64 return 0; 65 }

 

去除重复字符并排序

标签:

原文地址:http://www.cnblogs.com/hhboboy/p/5325119.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!