【九度OJ】题目1195:最长&最短文本 解题报告
标签(空格分隔): 九度OJ
http://ac.jobdu.com/problem.php?pid=1195
题目描述:
输入多行字符串,请按照原文本中的顺序输出其中最短和最长的字符串,如果最短和最长的字符串不止一个,请全部输出。
输入:
输入包括多行字符串,字符串的长度len,(1<=len<=1000)。
输出:
按照原文本中的顺序输出其中最短和最长的字符串,如果最短和最长的字符串不止一个,请全部输出。
样例输入:
hello
she
sorry
he
样例输出:
he
hello
sorry
Ways
其实还是很简单的,用二维数组保存字符串,这样的好处是第一维保存的是字符串的序号,第二维是字符串。再输入字符串的时候,记录下字符串的最短值和最长值。然后再遍历两遍字符串就好。
对了,不用排序。
#include<stdio.h>
#include<string.h>
int main() {
char in[1000][1020];
int min = 2000;
int max = 0;
int len = 0;
int count = 0;
while (scanf("%s", in[count]) != EOF) {
len = strlen(in[count]);
if (min > len) {
min = len;
}
if (max < len) {
max = len;
}
count++;
}
for (int i = 0; i < count; i++) {
int temp = strlen(in[i]);
if (temp == min) {
printf("%s\n", in[i]);
}
}
for (int i = 0; i < count; i++) {
int temp = strlen(in[i]);
if (temp == max) {
printf("%s\n", in[i]);
}
}
return 0;
}
Date
2017 年 3 月 19 日