然后输出前10个单词(按字母顺序),如果所有单词不超过10个,则将所有的单词输出。 输入样例 Failure is probably the fortification in your pole It is like a peek your wallet as the thief when you are thinking how to spend several hard-won lepta when you Are wondering whether new money it has laid background Because of you, then at the heart of the most lax alert and most low awareness and left it godsend failed !!!!!
时间: 2024-02-18 19:00:49 浏览: 184
好的,经过处理,输出的结果如下:
```
Are Because Failure It When a and are as background fortification godsend hard-won has heart how in is it laid lax lepta like low most new of peek pole probably several spend the thief thinking to wallet wondering you your
```
请注意,输出结果中的单词按照字母顺序排列,并且单词间用空格隔开。另外,输入样例中的标点符号也被认为是单词的一部分。
相关问题
现需要统计若干段文字(英文)中的不同单词数量。 如果不同的单词数量不超过10个,则将所有单词输出(按字母顺序),否则输出前10个单词。 注1:单词之间以空格(1个或多个空格)为间隔。 注2:忽略空行或者空格行。 注3:单词大小写敏感,即'word'与'WORD'是两个不同的单词 。 输入说明 若干行英文,最后以!!!!!为结束。 输出说明 不同单词数量。 然后输出前10个单词(按字母顺序),如果所有单词不超过10个,则将所有的单词输出。 输入样例 Failure is probably the fortification in your pole It is like a peek your wallet as the thief when you are thinking how to spend several hard-won lepta when you Are wondering whether new money it has laid background Because of you, then at the heart of the most lax alert and most low awareness and left it godsend failed !!!!! 输出样例 49 Are Because Failure It a alert and are as at 代码长度限制 16 KB 时间限制 400 ms 内存限制 64 MB
好的,我可以帮您编写一个 Java 程序来完成这个任务。以下是示例代码:
```java
import java.util.*;
public class WordCount {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String input = "";
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if (line.equals("!!!!!")) {
break;
}
input += line + " ";
}
// 将字符串按照空格分割为单词数组
String[] words = input.split("\\s+");
// 使用 TreeMap 来按照字母顺序排序
Map<String, Integer> wordCountMap = new TreeMap<>();
// 统计每个单词出现的次数
for (String word : words) {
if (!word.isEmpty()) { // 忽略空行或者空格行
if (wordCountMap.containsKey(word)) {
wordCountMap.put(word, wordCountMap.get(word) + 1);
} else {
wordCountMap.put(word, 1);
}
}
}
// 输出不同单词数量
System.out.println(wordCountMap.size());
// 输出前10个单词(按字母顺序)
int count = 0;
for (Map.Entry<String, Integer> entry : wordCountMap.entrySet()) {
if (count < 10) {
System.out.print(entry.getKey() + " ");
count++;
} else {
break;
}
}
}
}
```
运行程序,输入若干行英文,以"!!!!!"结束。程序会输出不同单词数量以及前10个单词(按字母顺序)。如果所有单词不超过10个,则将所有的单词输出。
阅读全文