PTA_7-4 找出最长的单词-hebust

题目描述:

找出长度最长的单词(不同长度的单词只出现一次)。

输入格式:

输入格式为单行形式,单词之间使用空格分割。

输出格式:

输出格式为长度最长的一个单词。

输入样例:

在这里给出一组输入。例如:

an not need happy suggest

输出样例:

在这里给出相应的输出。例如:

suggest

代码块:

import java.util.Scanner;

public class LogestWord {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String str = sc.nextLine();
		sc.close();
		
		String[] strs = str.split(" ");
		int max = strs[0].length();
		int k = 0;
		for(int i=0;i<strs.length;i++) {
			if(strs[i].length()>max) {
				max = strs[i].length();
				k = i;
			}
		}
		System.out.println(strs[k]);
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_45713984/article/details/106237758