保研练习题(3)

从键盘上任意输入一个长度不超过 20 的字符串,对所输入的字符串,按照 ASCII 码的大小从小到大进行排序,请输出排序后的结果。

 1 // 算法一
 2 #include<iostream>
 3 #include<string>
 4 #include<algorithm>
 5 using namespace std;
 6 void main()
 7 {
 8     string s;
 9     cout<<"Enter string :";
10     cin>>s;
11     sort(s.begin(),s.end());
12     cout<<s<<endl;
13 }

 1 // 算法二
 2 #include<stdio.h>
 3 #include <string.h>
 4 #include <stdlib.h>
 5 int cmp(const void *a,const void *b)
 6 {
 7     return *(char *)a-*(char *)b;
 8 }
 9 int main()
10 {
11     printf("Enter string :\n");
12     char s[25];
13     scanf("%s",s);
14     qsort(s,strlen(s),sizeof(s[0]),cmp);
15     puts(s);
16     return 0;
17 }

posted on 2013-09-15 21:00  RAUL_AC  阅读(216)  评论(0编辑  收藏  举报

导航