-
题目描述:
-
输入两个递增的序列,输出合并这两个序列后的递增序列。
-
输入:
-
每个测试案例包括3行:
第一行为1个整数n(1<=n<=1000000)表示这两个递增序列的长度。
第二行包含n个整数,表示第一个递增序列。
第三行包含n个整数,表示第二个递增序列。
-
输出:
-
对应每个测试案例,输出合并这两个序列后的递增序列。
-
样例输入:
-
41 3 5 72 4 6 8
-
样例输出:
-
1 2 3 4 5 6 7 8
import java.io.StreamTokenizer; import java.util.Arrays; import java.util.HashMap; public class Main { public static void main(String[] args) throws Exception { StreamTokenizer st = new StreamTokenizer(System.in); while (st.nextToken() != st.TT_EOF) { int n = (int) st.nval; int[] num1 = new int[n]; int[] num2 = new int[n]; for (int i = 0; i < n; i++) { st.nextToken(); num1[i] = (int) st.nval; } for (int i = 0; i < n; i++) { st.nextToken(); num2[i] = (int) st.nval; } int i = 0, j = 0; StringBuffer sb = new StringBuffer(); while (i < n && j < n){ if (num1[i] <= num2[j]) { sb.append(num1[i]); sb.append(" "); i++; } else { sb.append(num2[j]); sb.append(" "); j++; } } while (i < n){ sb.append(num1[i]); sb.append(" "); i++; } while (j < n){ sb.append(num2[j]); sb.append(" "); j++; } System.out.println(sb.toString().trim()); } } }