首页 > 试题广场 >

整型数组合并

[编程题]整型数组合并
  • 热度指数:136363 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
\hspace{15pt}对于给定的由 n 个整数组成的数组 \{a_1, a_2, \dots, a_n\}m 个整数组成的数组 \{b_1, b_2, \dots, b_m\},将它们合并后从小到大排序,并输出去重后的结果。

\hspace{15pt}注意,本题在输出时,元素间不需要输出空格。

输入描述:
\hspace{15pt}第一行输入一个整数 n\left(1 \leqq n \leqq 150 \right) 代表数组 a 中的元素个数。
\hspace{15pt}第二行输入 n 个整数 a_1, a_2, \dots, a_n \left(-1 \leqq a_i \leqq 10^5 \right) 代表数组 a 中的元素。
\hspace{15pt}第三行输入一个整数 m\left(1 \leqq m \leqq 150 \right) 代表数组 b 中的元素个数。
\hspace{15pt}第四行输入 m 个整数 b_1, b_2, \dots, b_m \left(-1 \leqq b_i \leqq 10^5 \right) 代表数组 b 中的元素。


输出描述:
\hspace{15pt}输出按升序合并、去重后的数组。
示例1

输入

3
1 2 5
4
-1 0 3 2

输出

-101235

说明

\hspace{15pt}在这个样例中,拼接后得到 \{1, 2, 5, -1, 0, 3, 2\},去重后得到 \{1, 2, 5, -1, 0, 3\},排序后得到 \{-1, 0, 1, 2, 3, 5\}
示例2

输入

1
11
1
111

输出

11111
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextInt()) {
            TreeSet<Integer> set = new TreeSet<Integer>();
            int n = in.nextInt();
            for (int i = 0; i < n; i++) {
                set.add(in.nextInt());
            }
            int m = in.nextInt();
            for (int i = 0; i < m; i++) {
                set.add(in.nextInt());
            }
            set.forEach(value -> {
                System.out.print(value);
            });
        }
    }
}
发表于 2025-03-27 16:58:22 回复(0)
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        Set<Integer> set = new HashSet<>();
        int n = in.nextInt();
        while (n-- > 0) set.add(in.nextInt());
        int m = in.nextInt();
        while (m-- > 0) set.add(in.nextInt());

        List<Integer> list = new ArrayList<>(set);
        list.stream().sorted((o1, o2)-> {return o1 - o2;}).forEach(System.out::print);
    }
}

发表于 2025-01-28 19:28:51 回复(0)
字符数组处理不了带符号的数字,那就升级为字符串数组,哈哈哈
发表于 2024-06-13 20:06:05 回复(0)
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        // 业务场景为不可重复,所以用set
        HashSet<Integer> set = new HashSet<>();
        for (int i = 0; i < 2; i++) {
            int n = Integer.parseInt(br.readLine());
            String input = br.readLine();
            String[] split = input.split(" ");
            for (int j = 0; j < split.length; j++) {
                set.add(Integer.parseInt(split[j]));
            }
        }


        // 转换为list
        ArrayList<Integer> list = new ArrayList<>(set);
        // 使用集合工具类排序
        Collections.sort(list);

        // 遍历打印
        for (Integer integer : list) {
            System.out.print(integer);
        }
    }
}

发表于 2023-08-13 10:38:51 回复(0)
import java.io.*;
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n1 = Integer.parseInt(br.readLine());
        String str1 = br.readLine();
        int n2 = Integer.parseInt(br.readLine());
        String str2 = br.readLine();
        StringBuilder sb1 = new StringBuilder();
        String[] strArr = sb1.append(str1).append(" ").append(str2).toString().split(" ");
        int[] arr = new int[n1 + n2];
        for (int i = 0; i < strArr.length; i++) arr[i] = Integer.parseInt(strArr[i]);
        Arrays.sort(arr);
        StringBuilder sb2 = new StringBuilder();
        sb2.append(arr[0]);
        for (int i = 0; i < arr.length - 1; i++) {
            if ((arr[i] != arr[i + 1])) sb2.append(arr[i + 1]);
        }
        System.out.println(sb2.toString());
    }
}

发表于 2023-07-06 16:21:40 回复(0)
 Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int a = in.nextInt();
            TreeSet<Integer> treeSet = new TreeSet<Integer>();
            for(int i=0;i<a;i++){
                int num = in.nextInt();
                treeSet.add(num);
            }
            int b = in.nextInt();
            for(int i=0;i<b;i++){
                int num = in.nextInt();
                treeSet.add(num);
            }
            Iterator it = treeSet.iterator();
            while(it.hasNext()){
               System.out.print(it.next());  
            }          
        }
发表于 2023-04-13 22:46:25 回复(0)
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n=in.nextInt();
        int[] a=new int[n];
        for(int i=0;i<n;i++)
        {
            a[i]=in.nextInt();
        }
        n=in.nextInt();
        int[] b=new int[n];
        for(int i=0;i<n;i++)
        {
            b[i]=in.nextInt();
        }
        Arrays.sort(a);
        Arrays.sort(b);
        int j=0;
        int k=0;
        int l=a.length+b.length;
        int[] c=new int[l];
        int m=0;
        while(j<a.length && k<b.length)
        {
            if(a[j]<b[k])
            {
               c[m]=a[j];
               j++;
               m++;
            }
            else if(a[j]>b[k])
            {
                c[m]=b[k];
                k++;
                m++;
            }
            else if(a[j]==b[k]){
                c[m]=a[j];
                j++;
                k++;
                m++;
            }
        }
        while(j<a.length)
        {
            c[m]=a[j];
            m++;
            j++;
        }
        while(k<b.length)
        {
            c[m]=b[k];
            m++;
            k++;
        }
        ArrayList<Integer> list = new ArrayList<>();
        for(int i=0;i<c.length;i++)
        {
            if(!list.contains(c[i]))
            {
                list.add(c[i]);
            }
        }
        StringBuffer sb=new StringBuffer();
        for(int i=0;i<list.size();i++)
        {
            sb.append(list.get(i));
        }
        System.out.println(sb.toString());
       
    }
}

这个笨办法为什么会在
6
2 8  3 6 3 2
6
6 3 6 2 8 11输出2368110,多一个“0”字符呢?
2 8 3 6 3 2
6
6 3 6 2 8 11
发表于 2023-03-03 18:43:30 回复(0)
用Treeset做,很快
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        int a=in.nextInt();
        Set<Integer> set=new TreeSet<>();
        for(int i=0; i<a;i++){
            set.add(in.nextInt());
        }
        int b=in.nextInt();
        for(int i=0; i<b; i++){
            set.add(in.nextInt());
        }
        for(Integer s: set){
            System.out.print(s);
        }
    }
}

发表于 2023-02-19 20:23:45 回复(0)
import java.util.Scanner;
import java.util.TreeSet;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        TreeSet<Integer> set = new TreeSet<>();
        while (in.hasNextInt()) {
            int a = in.nextInt();
            int[] arr = new int[a];
            for (int i = 0; i < a; i++){
                arr[i] = in.nextInt();
                set.add(arr[i]);
            }
        }
        for (int a : set){
            System.out.print(a);
        }
    }
}

发表于 2022-11-03 22:24:32 回复(0)
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int a = in.nextInt();
            Set<Integer> list = new TreeSet<>();
            while(a-->0) list.add(in.nextInt());
            int b = in.nextInt();
            while(b-->0) list.add(in.nextInt());
            for(Integer num:list)
                System.out.print(num);
        }
    }
}

发表于 2022-10-03 12:43:12 回复(0)
主要不让题目混淆了,不要以为是多组数组,只有两组。
发表于 2022-09-27 10:47:44 回复(0)
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int size1 = in.nextInt();
        int[] nums1 = new int[size1];
        for (int i = 0; i < size1; i++) {
            nums1[i] = in.nextInt();
        }
        int size2 = in.nextInt();
        int[] nums2 = new int[size2];
        for (int i = 0; i < size2; i++) {
            nums2[i] = in.nextInt();
        }

        List<Integer> list = new ArrayList<>();
        for (int num : nums1) {
            if (!list.contains(num)) {
                list.add(num);
            }

        }
        for (int num : nums2) {
            if (!list.contains(num)) {
                list.add(num);
            }
        }
        Collections.sort(list);
        list.forEach(e -> System.out.print(e));
    }
}

发表于 2022-09-25 20:37:49 回复(0)
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int m = sc.nextInt();
        sc.nextLine();
        Set<Integer> set = new TreeSet();
        for (int i = 0; i < m; i++) {
            set.add(sc.nextInt());
        }
        sc.nextLine();
        m = sc.nextInt();
        sc.nextLine();
        for (int i = 0; i < m; i++) {
            set.add(sc.nextInt());
        }
        for (Integer i : set) {
            System.out.print(i);
        }
        System.out.println();
    }
}
发表于 2022-07-10 16:10:42 回复(0)
import java.util.*;  public class Main { public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);  while (sc.hasNext()) {
            Set<Integer> array1 = new HashSet<>();  Set<Integer> array2 = new HashSet<>();  int num = sc.nextInt();  for (int i = 0; i < num; i++) {
                array1.add(sc.nextInt());  } int num2 = sc.nextInt();  for (int i = 0; i < num2; i++) {
                array2.add(sc.nextInt());  }
            array1.addAll(array2);  Object[] objects = array1.toArray();  Arrays.sort(objects);  for (Object object : objects) {
                System.out.print(object);  }
        }
    }
}
发表于 2022-06-26 18:47:20 回复(0)
import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int length1 = sc.nextInt();

        TreeSet<Integer> set = new TreeSet<>();
        for (int i = 0; i < length1; i++) {
            set.add(sc.nextInt());
        }

        int length2 = sc.nextInt();
        for (int i = 0; i < length2; i++) {
            set.add(sc.nextInt());
        }

        for (Integer value : set) {
            System.out.print(value);
        }
    }
}
treeSet 不仅能去重,还能自然排序
发表于 2022-06-17 23:26:43 回复(0)
发表于 2022-06-15 14:28:49 回复(0)
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        TreeSet<Integer> set = new TreeSet();
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int num = in.nextInt();
            for(int i=0;i<num;i++){
                int a = in.nextInt();
                set.add(a);
            }            
        }
        
        for(int b : set){
            System.out.print(b);
        }
    }
}
发表于 2022-06-04 18:17:45 回复(0)
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner in=new Scanner((System.in));
        while(in.hasNext()){
            int n1=in.nextInt();
            int arr1[]=new int [n1];
            in.nextLine();
            for(int i=0;i<n1;i++)
                arr1[i]=in.nextInt();
            in.nextLine();
            int n2=in.nextInt();
            int arr2[]=new int [n2];
            for(int i=0;i<n2;i++)
                arr2[i]=in.nextInt();
            TreeSet<Integer> ts=new TreeSet<>();
            for(int i:arr1)
                ts.add(i);
            for(int i:arr2)
                ts.add(i);
            for(int i:ts){
                System.out.print(i);
            }
            System.out.println();
        }
    }
}

发表于 2022-05-30 20:33:05 回复(0)
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int x = sc.nextInt();
        Set list = new TreeSet();
        for(int i = 0;i < x;i++){
            int jp = sc.nextInt();
            list.add(jp);
        }
        int y = sc.nextInt();
        for(int i = 0;i < y;i++){
            int jp = sc.nextInt();
            list.add(jp);
        }
        for(Object o : list){
            System.out.print(o);
        }
    }
}

发表于 2022-05-29 16:26:11 回复(0)