|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
输入包括多行字符串,字符串的长度len(1<=len<=1000)。按原文本中的顺序输出其中最短和最长的字符串,如果最短和最长的字符串不止一个,全部输出。
例:
输入:
输出:
有一点要注意的就是没有说字符串有没有空格,所以不用scanf输入字符串,用gets函数。
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- typedef struct String
- {
- char str[1000];
- int length;
- }String;
- String strings[100]; //假设最多100个字符串
- int main()
- {
- char ch;
- //char longest[1000] = {0}, shortest[1000] = {0};
- int i = 0;
- while(gets(strings[i].str))//字符串可能有空格,所以不能用scanf,用gets接收
- {
- strings[i].length = strlen(strings[i].str);
- i++;
- }//一共输入了i个字符串
- int l, s;
- l = s = strings[0].length;//初始化最长最短字符串的长度
- int count_short[100] = {0}, count_long[100] = {100};//记录最长最短字符串的位置
- int count_s = 0, count_l = 100;//最长最短字符串的标记位置
- for(int j = 1; j < i; j++)
- {
- if(strings[j].length > l)//找到了比最长字符串更长的
- {
- count_l++;
- count_long[j] = count_l;
- l = strings[j].length;
- }
- else if(strings[j].length < s)//找到了比最短字符串更短的
- {
- count_s++;
- count_short[j] = count_s;
- s = strings[j].length;
- }
- else if(strings[j].length == l)//找到了与最长字符串等长的
- {
- count_long[j] = count_l;
- }
- else if(strings[j].length == s)//找到了与最短字符串等长的
- {
- count_short[j] = count_s;
- }
- }//循环结束时count_s和count_l记录的是最短最长字符串的标记位置
- for(int k = 0; k < i; k++)
- {
- if(count_short[k] == count_s)
- printf("%s\n", strings[k].str);
- }
- for(int k = 0; k < i; k++)
- {
- if(count_long[k] == count_l)
- printf("%s\n", strings[k].str);
- }
-
- return 0;
- }
复制代码 之所以将count_s和count_l设置成0和100,是为了防止输出时的识别错误导致输出错误。
其实我设置的tag有点多,没有必要设置这么多tag
|
|