九度OJ 题目1135:字符串排序
时间限制:1 秒
内存限制:32 兆
特殊判题:否
提交:518
解决:254
先输入你要输入的字符串的个数。然后换行输入该组字符串。每个字符串以回车结束,每个字符串少于一百个字符。
如果在输入过程中输入的一个字符串为“stop”,也结束输入。
然后将这输入的该组字符串按每个字符串的长度,由小到大排序,按排序结果输出字符串。
字符串的个数,以及该组字符串。每个字符串以‘\n’结束。如果输入字符串为“stop”,也结束输入.
可能有多组测试数据,对于每组数据,
将输入的所有字符串按长度由小到大排序输出(如果有“stop”,不输出“stop”)。
5sky is greycoldvery coldstop3it is good enough to be proud ofgoodit is quite good
coldvery coldsky is greygoodit is quite goodit is good enough to be proud of
根据输入的字符串个数来动态分配存储空间(采用new()函数)。每个字符串会少于100个字符。
测试数据有多组,注意使用while()循环输入。
/********************************** 日期:2013-2-13* 作者:SJF0115* 题号: 九度OJ 题目1135:字符串排序* 来源:http://ac.jobdu.com/problem.php?pid=1135* 结果:AC* 来源:2008年北京大学软件所计算机研究生机试真题* 总结:**********************************/#include<stdio.h>#include<stdlib.h>#include<string.h>#include <stdio.h>#include <stdlib.h>#include <string.h>//字符串结构体typedef struct String{char str[100];//字符串int len;//长度}String;//排序函数int cmp(const void *a, const void *b){struct String *c = (String *)a; struct String *d = (String *)b;return c->len - d->len;}int main(){int i,j,n;String *strs;while(scanf("%d\n", &n) != EOF){//接收回车符//getchar();//初始化结构体strs = (String *)malloc(sizeof(String) * n);for(i = 0;i < n;i++){//输入字符串gets(strs[i].str);//计算字符串长度strs[i].len = strlen(strs[i].str);//如果在输入过程中输入的一个字符串为“stop”,也结束输入。if(strcmp(strs[i].str,"stop") == 0){break;}}//按字符串长度排序qsort(strs,i,sizeof(strs[0]),cmp);//输出for(j = 0;j < i;j++){puts(strs[j].str);}}return 0;}