import java.util.HashSet; import java.util.Scanner; import java.util.Set; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String input = scanner.nextLine(); String[] numbers = input.split(" "); Set<Integer> set = new HashSet<>(); for (String number : numbers) { int num = Integer.parseInt(number); if (!set.add(num)) { System.out.println("yes"); return; } } System.out.println("no"); }加解释
时间: 2024-02-15 18:27:39 浏览: 29
这段代码实现了一个功能:读入一行输入,将其中的数字以空格分隔开,判断是否存在重复数字。
具体实现步骤:
1. 首先创建一个 Scanner 对象,读取一行输入。
2. 使用 String 类的 split 方法,将输入字符串按照空格分隔成多个字符串,存储在一个字符串数组中。
3. 创建一个 HashSet 对象,用于存储已经出现过的数字。
4. 遍历字符串数组中的每一个字符串,使用 Integer 类的 parseInt 方法将其转换成整数。
5. 判断这个整数是否已经存在于 HashSet 中,如果已经存在,则说明存在重复数字,输出 "yes" 并结束程序。
6. 如果整个字符串数组都遍历完毕,没有发现重复数字,则输出 "no"。
需要注意的是,这里使用 HashSet 来存储已经出现过的数字,是因为 HashSet 具有自动去重的功能,可以方便地判断一个数字是否已经出现过。如果使用其他数据结构,比如数组或者列表,需要手动进行去重操作,会增加代码的复杂度和运行时间。
相关问题
import java.util.ArrayList; import java.util.Collections; import java.util.Scanner; import java.util.Hashtable; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); Set set = new LinkedHashSet(); String a; String b = sc.nextLine(); for(int i =0 ;i <n;i++){ set.add(sc.nextLine()); } System.out.println(set.size()); for(Object obj:set){ System.out.println(obj); } sc.close(); } }
这段代码实现了从标准输入中读取n个字符串,并去重后输出不重复字符串个数和每个字符串。具体实现过程如下:
1. 导入需要使用的包,包括`java.util.Scanner`、`java.util.Set`和`java.util.LinkedHashSet`。
2. 创建一个`Scanner`对象`sc`,用于从标准输入中读取数据。
3. 从标准输入中读取一个整数`n`,表示接下来要读取的字符串个数。
4. 创建一个`LinkedHashSet`对象`set`,用于存储不重复的字符串。
5. 使用`for`循环,循环读取`n`个字符串,并将其添加到`set`中。
6. 输出不重复字符串的个数,即`set`的大小。
7. 使用`for`循环遍历`set`中的每个元素,输出每个字符串。
8. 关闭`Scanner`对象。
这段代码中使用了`LinkedHashSet`来实现去重功能,与`HashSet`相比,`LinkedHashSet`能够保持元素的插入顺序,因此在输出时能够保证按照输入顺序输出。另外,由于`LinkedHashSet`继承自`HashSet`,因此也具有去重的功能。
Duplicated Numbers 分数 10 作者 翁恺 单位 浙江大学 This program reads a lot of integers, in which may be duplicated numbers. The program picks out all the duplicated ones and sorts the remainders in a descendent order. 函数接口定义: public static ArrayList<Integer> pick(ArrayList<Integer> a); a is the ArrayList to be parsed and returns the result as an ArrayList. Hint: These classes and functions may be used. Collection Collections.sort(Collection); Collection Collections.reverse(Collection) 裁判测试程序样例: import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; import java.util.Scanner; public class Main { /* 请在这里填写答案 */ public static void main(String[] args) { ArrayList<Integer> lst = new ArrayList<>(); Scanner in = new Scanner(System.in); while ( in.hasNextInt() ) { lst.add(in.nextInt()); } lst = pick(lst); for ( int x:lst) { System.out.print(x+" "); } System.out.println(); in.close(); } }
下面是实现题目要求的代码:
```
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
public class Main {
public static ArrayList<Integer> pick(ArrayList<Integer> a) {
ArrayList<Integer> result = new ArrayList<Integer>();
HashSet<Integer> set = new HashSet<Integer>();
for (int i = 0; i < a.size(); i++) {
if (set.contains(a.get(i))) {
result.add(a.get(i));
} else {
set.add(a.get(i));
}
}
Collections.sort(result);
Collections.reverse(result);
return result;
}
public static void main(String[] args) {
ArrayList<Integer> lst = new ArrayList<>();
Scanner in = new Scanner(System.in);
while (in.hasNextInt()) {
lst.add(in.nextInt());
}
lst = pick(lst);
for (int x : lst) {
System.out.print(x + " ");
}
System.out.println();
in.close();
}
}
```