您的位置:首页 > 其它

九度OJ 1195:最长&最短文本 (搜索)

2015-10-26 11:33 344 查看
时间限制:1 秒

内存限制:32 兆

特殊判题:否

提交:3144

解决:1156

题目描述:

输入多行字符串,请按照原文本中的顺序输出其中最短和最长的字符串,如果最短和最长的字符串不止一个,请全部输出。

输入:

输入包括多行字符串,字符串的长度len,(1<=len<=1000)。

输出:

按照原文本中的顺序输出其中最短和最长的字符串,如果最短和最长的字符串不止一个,请全部输出。

样例输入:
hello
she
sorry
he


样例输出:
he
hello
sorry


来源:2008年华中科技大学计算机研究生机试真题

思路:

先搜索一遍,知道最短长度和最长长度,然后遍历输出。

也可排序后输出。

代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define N 1000

int cmp(const void *a, const void *b)
{
return strlen((char *)a) - strlen((char *)b);
}

int main(void)
{
int n;
int i, j;
char s
[N+1];

i = 0;
while (scanf("%s", s[i]) != EOF)
i++;

n = i;
qsort(s, n, sizeof(s[0]), cmp);
for (i=1; i<n && strlen(s[i])==strlen(s[0]); i++) ;
for (j=0; j<i; j++)
printf("%s\n", s[j]);
if (strlen(s[0]) != strlen(s[n-1]))
{
for (i=n-2; i>=0 && strlen(s[i])==strlen(s[n-1]); i--) ;
for (j=i+1; j<n; j++)
printf("%s\n", s[j]);
}

return 0;
}
/**************************************************************
Problem: 1195
User: liangrx06
Language: C
Result: Accepted
Time:10 ms
Memory:1820 kb
****************************************************************/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: