首页 > 试题广场 >

最长&最短文本

[编程题]最长&最短文本
  • 热度指数:9889 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
输入多行字符串,请按照原文本中的顺序输出其中最短和最长的字符串,如果最短和最长的字符串不止一个,请全部输出。

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


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

输入

hello
she
sorry
he

输出

he
hello
sorry
#include <stdio.h>

///KY234 最长&最短文本
typedef struct str {
    char s[1001];
    int len;
} str;
int main() {
    str a[1001], temp;
    char x;
    int t = 0, i, j;
    while (scanf("%c", &x) != EOF) {
        i = 0;
        while (x != '\n') {
            a[t].s[i] = x;
            i++;
            scanf("%c", &x);
        }
        a[t].len = strlen(a[t].s);
        t++;
    }
    for (i = 0; i < t; i++)
        for (j = 0; j < t - 1 - i; j++)
            if (a[j].len > a[j + 1].len) {
                temp = a[j];
                a[j] = a[j + 1];
                a[j + 1] = temp;
            }
    for (i = 0; i < t; i++) {
        if (a[i].len == a[0].len)
            printf("%s\n", a[i].s);
        else if (a[i].len == a[t - 1].len)
            printf("%s\n", a[i].s);
    }
    return 0;
}


发表于 2023-01-30 14:42:08 回复(0)