- 题目描述:
-
先输入你要输入的字符串的个数。然后换行输入该组字符串。每个字符串以回车结束,每个字符串少于一百个字符。
如果在输入过程中输入的一个字符串为“stop”,也结束输入。
然后将这输入的该组字符串按每个字符串的长度,由小到大排序,按排序结果输出字符串。
- 输入:
-
字符串的个数,以及该组字符串。每个字符串以‘ ’结束。如果输入字符串为“stop”,也结束输入.
- 输出:
-
可能有多组测试数据,对于每组数据,
将输入的所有字符串按长度由小到大排序输出(如果有“stop”,不输出“stop”)。
- 样例输入:
-
5 sky is grey cold very cold stop 3 it is good enough to be proud of good it is quite good
- 样例输出:
-
cold very cold sky is grey good it is quite good it is good enough to be proud of
1 #include <cstdio> 2 #include <cstdlib> 3 #include <cstring> 4 #include <string> 5 #include <algorithm> 6 #define MAX 109 7 struct Sen 8 { 9 int len; 10 int id; 11 }; 12 13 Sen toS[MAX]; 14 15 int cmp(const void * a, const void *b) { 16 int at = (*(Sen *)a).len; 17 int bt = (*(Sen *)b).len; 18 return at - bt; 19 } 20 21 int main(int argc, char const *argv[]) 22 { 23 //freopen("input.txt","r",stdin); 24 int n; 25 while(scanf("%d",&n) != EOF) { 26 char **temp = new char*[n]; 27 getchar(); 28 for(int i = 0; i < n; i++) { 29 temp[i] = new char[MAX]; 30 gets(temp[i]); 31 if(strcmp(temp[i],"stop") == 0) { 32 n = i; 33 delete temp[i]; 34 break; 35 } 36 toS[i].len = strlen(temp[i]); 37 toS[i].id = i; 38 } 39 qsort(toS, n, sizeof(Sen), cmp); 40 for(int i = 0; i < n; i++) { 41 printf("%s ",temp[toS[i].id]); 42 } 43 for(int i = 0; i < n; i++) { 44 delete temp[i]; 45 } 46 delete temp; 47 } 48 return 0; 49 }