大二算法题、有点不会思路 有很多c++的代码 但没有java的,所以想问问大家
论文问答
1
-
import java.util.HashMap; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int T = sc.nextInt(); for(int i = 0;i < T;i++){ int N = sc.nextInt(),n = 0; int arr[] = new int[N]; HashMap<Integer,Integer> map = new HashMap<>(); for(int j = 0;j < N;j++){ int t = sc.nextInt(); if(!map.containsKey(t)){ map.put(t, 1); arr[n++] = t; } } for(int j = 0;j < n;j++){ System.out.print(arr[j] + " "); } System.out.println(); } } }
-
public class Test { /* 2 11 1 2 18 3 3 19 2 3 6 5 4 6 1 2 3 4 5 6 */ public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int t = scanner.nextInt(); while (t-- > 0) { int n = scanner.nextInt(); Set<Integer> set = new LinkedHashSet<>(); for (int i = 0; i < n; i++) { set.add(scanner.nextInt()); } for (int v : set) { System.out.print(v + " "); } System.out.println(); } scanner.close(); } }
发表回复