1395 - 最长&最短文本

通过次数

0

提交次数

0

时间限制 : 1 秒 内存限制 : 32 MB

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

题目输入

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

题目输出

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

输入/输出样例

输入格式

hello
she
sorry
he

输出格式

he
hello
sorry

C语言解答

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
	char **str;
	int *len;
	int strlength;
	int i=0,j;
	int lenmin,lenmax;
	str = (char **)malloc(sizeof(char *)*1);
	str[i] = (char *)malloc(sizeof(char)*1001);
	len = (int *)malloc(sizeof(int)*1);
// 	while(i<5)
	while(gets(str[i]))
// 	while(scanf("%s",str[i]) != EOF)
	{
/*		gets(str[i]);*/
		strlength = strlen(str[i]);
		if(i==0)
		{
			lenmin = lenmax = strlength;
		}
		else
		{
			if(strlength < lenmin )
			{
				lenmin = strlength;
			}
			if(strlength > lenmax)
			{
				lenmax = strlength;
			}
		}
		len[i] = strlength;
		i++;
		str = (char**)realloc(str,sizeof(char *)*(i+1));
		str[i] = (char*)malloc(sizeof(char)*1001);
		len = (int *)realloc(len,sizeof(int)*(i+1));
	}
	for(j=0;j<i;j++)
	{
		if(len[j] == lenmin)
		{
			puts(str[j]);
		}
	}
	for(j=0;j<i;j++)
	{
		if(len[j] == lenmax)
		{
			puts(str[j]);
		}
	}
	free(len);
	for(j=0;j<i;j++)
	{
		free(str[j]);
	}
	free(str);
	return 0;
}

C++解答

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

char str[1000][1100]; 	// 定义储存字符串的变量
int count[1000];		// 定义记录字符串长度的变量

int main(){
	int numOfStrings = 0;	// 记录字符串的总数
	int maxValue=0x80000000, minValue=0x7FFFFFFF;	// 还记得求最大值最小值时如何定义的吗
	while(gets(str[numOfStrings])){	// 读取字符串
		count[numOfStrings] = strlen(str[numOfStrings]);
		if(maxValue < count[numOfStrings]){
			maxValue = count[numOfStrings];		// 获得最大值
		}
		if(minValue > count[numOfStrings]){
			minValue = count[numOfStrings];		// 获得最小值
		}
		numOfStrings++;							// 注意字符串的数目增加了
	}

	// 下面输出长度最小的字符串
	for(int i=0; i<numOfStrings; i++){
		if(count[i] == minValue){
			puts(str[i]);
		}
	}

	// 下面输出长度最大的字符串
	for(int i=0; i<numOfStrings; i++){
		if(count[i] == maxValue){
			puts(str[i]);
		}
	}

	return 0;
}

Java解答

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		//字符串数组
        ArrayList<String> list = new ArrayList<>();
        //字符串长度数组
        ArrayList<Integer> list1 = new ArrayList<>(); 
 	
        while (sc.hasNextLine()) {
            String string = sc.nextLine();
            if ("".equals(string)) {
                break; 
            }
 
            list.add(string);//将键盘接收的字符串添加到字符串数组中
            list1.add(string.length());//将字符串的长度添加到list1中
        }
        Collections.sort(list1); //对所有字符串的长度进行排序
 	//用for循环遍历字符串数组的所有元素,查看其长度与list1中最大最小的值是否相等
	 //若相等便输出
	//查找最小长度字符串
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).length() == list1.get(0)) {
                System.out.println(list.get(i));
            }
        }
        //查找最大长度字符串
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).length() == list1.get(list1.size() - 1)) {
                System.out.println(list.get(i));
            } 
        }
}
}