输入两个升序排列的序列,将两个序列合并为一个有序序列并输出。
数据范围: , 序列中的值满足
输入包含三行,
第一行包含两个正整数n, m,用空格分隔。n表示第二行第一个升序序列中数字的个数,m表示第三行第二个升序序列中数字的个数。
第二行包含n个整数,用空格分隔。
第三行包含m个整数,用空格分隔。
输出为一行,输出长度为n+m的升序序列,即长度为n的升序序列和长度为m的升序序列中的元素重新进行升序序列排列合并。
5 6 1 3 7 9 22 2 8 10 17 33 44
1 2 3 7 8 9 10 17 22 33 44
import java.util.Scanner; import java.util.Arrays; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(), m = in.nextInt(); int[] nums1 = new int[n]; int[] nums2 = new int[m]; //输入两个数组 for (int i = 0; i < n; i++) { nums1[i] = in.nextInt(); } for (int i = 0; i < m; i++) { nums2[i] = in.nextInt(); } //合并两个数组 int[] nums3 = new int[m + n]; for (int i = 0; i < n; i++) { nums3[i] = nums1[i]; } for (int i = 0; i < m; i++) { nums3[i + n] = nums2[i]; } //排序(Java自带,升序排序) Arrays.sort(nums3); //打印 for (int i = 0; i < m + n; i++) { System.out.print(nums3[i] + " "); } } }
import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int m = in.nextInt(); int[] arr = new int[n + m]; for(int i = 0; i < n + m; i++) { arr[i] = in.nextInt(); } Arrays.sort(arr); for(int i = 0; i < n + m; i++) { System.out.print(arr[i] + " "); } } }
import java.util.LinkedList; import java.util.ListIterator; import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); LinkedList<Integer> list1 = new LinkedList<>(); LinkedList<Integer> list2 = new LinkedList<>(); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextInt()) { // 注意 while 处理多个 case int n = in.nextInt(); int m = in.nextInt(); for (int i = 0; i < n; i++) { list1.add(in.nextInt()); } for (int i = 0; i < m; i++) { list2.add(in.nextInt()); } if (list1 == null) { for (int a : list2) { System.out.printf("%d ", a); break; } continue; } if (list2 == null) { for (int a : list1) { System.out.printf("%d ", a); break; } continue; } LinkedList<Integer> mergedList = new LinkedList<>(); ListIterator iter1 = list1.listIterator(); ListIterator iter2 = list2.listIterator(); while (iter1.hasNext() && iter2.hasNext()) { Integer item1 = (Integer)iter1.next(); Integer item2 = (Integer)iter2.next(); if (item1 <= item2) { mergedList.addLast(item1); iter2.previous(); } else { mergedList.addLast(item2); iter1.previous(); } } while (iter1.hasNext()) { mergedList.addLast((Integer)iter1.next()); } while (iter2.hasNext()) { mergedList.addLast((Integer)iter2.next()); } for (int a : mergedList) { System.out.printf("%d ", a); } } } }
import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int a = in.nextInt(); int b = in.nextInt(); int[] arrA = new int[a + b]; while (in.hasNextInt()) { for (int i = 0; i < arrA.length; i++) { arrA[i] = in.nextInt(); } } Arrays.sort(arrA); for(int i=0;i<arrA.length;i++){ System.out.print(arrA[i]+" "); } } }
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc=new Scanner(System.in); int n=sc.nextInt(),m=sc.nextInt(); int[] arr=new int[n+m]; for(int i=0;i<n+m;i++) arr[i]=sc.nextInt(); Arrays.sort(arr); for(int num:arr) System.out.printf("%d ",num); } }
import java.util.*; public class Main { //方法:冒泡排序(从小到大顺序) public static int[] maopao(int[] arr) { int temp = 0; for (int i = 0; i < arr.length - 1; i++) { for (int j = 0; j < arr.length - 1 - i; j++) { if (arr[j] > arr[j + 1]) { temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } return arr; } //主程序:输入总数组,调用冒泡,输出 public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); int[] arr = new int[n+m];//直接合并成一个数组输入 for (int i = 0; i < n+m; i++) { arr[i] = sc.nextInt(); } int[] arr1 = maopao(arr); //遍历排序后的新数组并输出,注意加" " for (int a:arr1) { System.out.print(a+" "); } } }
import java.util.*; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while(scanner.hasNext()){ int n = scanner.nextInt(); int m = scanner.nextInt(); List<Integer> nRowCountNumList = new ArrayList<>(); List<Integer> mRowCountNumList = new ArrayList<>(); List<Integer> afterSortList = new ArrayList<>(); for(int i = 0;i < n;i++){ nRowCountNumList.add(scanner.nextInt()); afterSortList.add(nRowCountNumList.get(i)); } for(int j = 0;j < m;j++){ mRowCountNumList.add(scanner.nextInt()); afterSortList.add(mRowCountNumList.get(j)); } //排序n和m中合并的集合 Collections.sort(afterSortList); for(int k = 0;k < afterSortList.size();k++){ System.out.print(afterSortList.get(k) + " "); } } } }
import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); List<Integer> list1 = new ArrayList<>(); List<Integer> list2 = new ArrayList<>(); List<Integer> newList = new ArrayList<>(); for (int i = 0; i < n; i++) { list1.add(sc.nextInt()); newList.add(list1.get(i)); } for (int i = 0; i < m; i++) { list2.add(sc.nextInt()); newList.add(list2.get(i)); } Collections.sort(newList); for (int i = 0; i < newList.size(); i++) { System.out.print(newList.get(i) + " "); } } }
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); String[] num = sc.nextLine().split(" "); String[] arr1 = sc.nextLine().split(" "); String[] arr2 = sc.nextLine().split(" "); List<Integer> list = new ArrayList<>(); for(String i:arr1) list.add(Integer.valueOf(i)); for(String j:arr2) list.add(Integer.valueOf(j)); Collections.sort(list); StringBuilder sb = new StringBuilder(); for(Integer v:list){ sb.append(Integer.toString(v)+" "); } System.out.println(sb.toString()); } }
import java.util.Scanner; import java.util.Arrays; public class Main{ public static void main(String args[]){ Scanner scan=new Scanner(System.in); int res[]=new int[scan.nextInt()+scan.nextInt()]; for(int i=0;i<res.length;i++){ res[i]=scan.nextInt(); } Arrays.sort(res); for(int i=0;i<res.length;i++){ System.out.print(res[i]+" "); } } }这没啥难的,输入到一个数组里就行了
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { int n = sc.nextInt(); int m = sc.nextInt(); //把合并后的数组都放到第一个数组里 int[] arr1 = new int[n+m]; int[] arr2 = new int[m]; for (int i = 0; i < n; i++) { arr1[i] = sc.nextInt(); } for (int i = 0; i < m; i++) { arr2[i] = sc.nextInt(); } mergeSort(arr1,arr2,n-1,m-1); StringBuilder res = new StringBuilder(); for (int i = 0; i < arr1.length; i++) { res.append(arr1[i]); if (i!=arr1.length-1)res.append(" "); } System.out.println(res.toString()); } } //归并排序,合并数组1和数组2 public static void mergeSort(int[] arr1,int[] arr2,int a,int b){ if (arr2 == null || arr2.length == 0){ return; } int tail = arr1.length-1; while(a>=0 && b >=0){ if (arr1[a]>arr2[b]){ arr1[tail--] = arr1[a--]; }else{ arr1[tail--] = arr2[b--]; } } while(a>=0){ arr1[tail--] = arr1[a--]; } while(b>=0){ arr1[tail--] = arr2[b--]; } } }