九度-题目1195:最长&最短文本

http://ac.jobdu.com/problem.php?pid=1195

题目描述:

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

输入:

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

输出:

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

样例输入:
hello
she
sorry
he
样例输出:
he
hello
sorry
来源:
2008年华中科技大学计算机研究生机试真题
只有一组输入,用cltr+z结束。在输入阶段找到最大长度和最小长度。
之后遍历所有字符串,输出最短和最长的字符串。
 1 #include <iostream>
 2 #include  <stdio.h>
 3 #include <string>
 4 
 5 #define INF 2000
 6 
 7 using namespace std;
 8 
 9 const int N=1000;
10 string s[N];
11 
12 
13 int main()
14 {
15     int size=0;
16     int len=0;
17     int lmax=0;
18     int lmin=INF;
19     while(cin>>s[size])
20     {
21         len=s[size].size();
22         if(len>lmax)
23             lmax=len;
24         if(len<lmin)
25             lmin=len;
26         size++;
27     }
28 
29     for(int i=0; i<size; i++)
30     {
31         if(s[i].size()==lmin)
32             cout << s[i] <<endl;
33     }
34 
35     for(int i=0; i<size; i++)
36     {
37         if(s[i].size()==lmax)
38             cout << s[i] <<endl;
39     }
40 
41     return 0;
42 }
原文地址:https://www.cnblogs.com/shenckicc/p/6756094.html