解题思路:
注意事项:
参考代码:
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int a[] = new int[10000]; for (int i = 0; i < n; i++) a[i] = sc.nextInt(); int m = sc.nextInt(); int b[] = new int[10000]; for (int i = 0; i < m; i++) b[i] = sc.nextInt(); /** 交集 */ int c[] = new int[10000]; int t = 0; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if (a[i] == b[j]) { c[t] = a[i]; t++; } Arrays.sort(c, 0, t); for (int i = 0; i < t; i++) System.out.print(c[i] + " "); System.out.println(); /** 并集 */ int d[] = new int[10000]; for (int i = 0; i < n; i++) d[i] = a[i]; for (int i = n; i < n + m; i++) d[i] = b[i - n]; Arrays.sort(d, 0, n + m); for (int i = 0; i < n + m; i++) if (d[i] == d[i + 1]) continue; else System.out.print(d[i] + " "); System.out.println(); /** 补集 */ Arrays.sort(a, 0, n); int e[] = new int[10000]; int k = 0; for (int i = 0; i < n; i++) { boolean f = true; for (int j = 0; j < t; j++) { if (a[i] == c[j]) { f = false; break; } } if (f) { e[k] = a[i]; k++; } } for (int i = 0; i < e.length; i++) if (e[i] != 0) System.out.print(e[i] + " "); } }
0.0分
0 人评分
C语言程序设计教程(第三版)课后习题5.7 (C语言代码)浏览:669 |
简单的a+b (C语言代码)浏览:564 |
P1001 (C语言代码)浏览:836 |
WU-输出正反三角形 (C++代码)浏览:1099 |
WU-小九九 (C++代码)浏览:1713 |
C语言程序设计教程(第三版)课后习题6.3 (C++代码)浏览:1067 |
sizeof的大作用 (C语言代码)浏览:1592 |
蛇行矩阵 (C语言代码)浏览:559 |
IP判断 (C语言代码)浏览:592 |
妹子杀手的故事 (C语言代码)浏览:1153 |