1. 程式人生 > >在線編程筆試練習2(京東)

在線編程筆試練習2(京東)

delete while red integer sca trees buffer port 和集

時間限制:1秒 空間限制:32768K 熱度指數:9801

題目描述

給你兩個集合,要求{A} + {B}。 註:同一個集合中不會有兩個相同的元素。

輸入描述:

每組輸入數據分為三行,第一行有兩個數字n,m(0 ≤ n,m ≤ 10000),分別表示集合A和集合B的元素個數。後兩行分別表示集合A和集合B。每個元素為不超過int範圍的整數,每個元素之間有個空格隔開。

輸出描述:

針對每組數據輸出一行數據,表示合並後的集合,要求從小到大輸出,每個元素之間有一個空格隔開,行末無空格。
示例1

輸入

3 3
1 3 5
2 4 6

輸出

1 2 3 4 5 6

自己的low方法(沒通過)
技術分享
 1
import java.util.Scanner; 2 3 /* 4 * To change this license header, choose License Headers in Project Properties. 5 * To change this template file, choose Tools | Templates 6 * and open the template in the editor. 7 */ 8 9 /** 10 * 11 * @author zhangtao 12 */ 13 public class Test2 { 14 public
static void main(String[] args) 15 { 16 int m,n; 17 int[] A; 18 int[] B; 19 Scanner scanner=new Scanner(System.in); 20 m=scanner.nextInt(); 21 n=scanner.nextInt(); 22 A=new int[m]; 23 B=new int[n]; 24 //錄入數據 25 for(int
i=0;i<m&&scanner.hasNext();i++) 26 { 27 A[i]=scanner.nextInt(); 28 }for(int j=0;j<m&&scanner.hasNext();j++) 29 { 30 B[j]=scanner.nextInt(); 31 } 32 //按要求排序輸出 33 sortAandB(A,B); 34 } 35 static void sortAandB(int[] A,int[] B) 36 { 37 int totallong=A.length+B.length; 38 int[]C=new int[totallong]; 39 //將A與B合並 40 int i=0; 41 while(i<A.length) 42 { 43 C[i]=A[i]; 44 i++; 45 } 46 while(i>=A.length&&i<totallong) 47 { 48 C[i]=B[i-A.length]; 49 i++; 50 } 51 quickSort(C,0,totallong-1) ; 52 for(int j=0;j<totallong;j++) 53 { 54 if(j!=totallong-1) 55 { 56 System.out.print(C[j]+" "); 57 } 58 else 59 { 60 System.out.print(C[j]+""); 61 } 62 } 63 } 64 //快速排序 65 static int partition(int a[], int low, int high) { 66 int privotKey = a[low]; //基準元素 67 while (low < high) { //從表的兩端交替地向中間掃描 68 while (low < high && a[high] >= privotKey) //從high 所指位置向前搜索,至多到low+1 位置。將比基準元素小的交換到低端 69 { 70 --high; //從右找比基準元小的 71 } 72 a[low] = a[high]; //如果比基準元素小,交換 73 a[high] = privotKey; 74 75 while (low < high && a[low] <= privotKey) { 76 ++low; //從右找比基準元大的 77 } 78 a[high] = a[low]; //如果比基準元素,交換 79 a[low] = privotKey; 80 81 } 82 return low; 83 } 84 static void quickSort(int a[], int low, int high) { 85 if (low < high) { 86 int privotLoc = partition(a, low, high); //將表一分為二 87 quickSort(a, low, privotLoc - 1); //遞歸對低子表遞歸排序 88 quickSort(a, privotLoc + 1, high); //遞歸對高子表遞歸排序 89 } 90 } 91 }
View Code

大神方案一

技術分享
 1 import java.util.Iterator;
 2 import java.util.Scanner;
 3 import java.util.Set;
 4 import java.util.TreeSet;
 5 //集合合並
 6 public class Test2 {
 7  
 8     public static void main(String[] args) {
 9         Scanner scan=new Scanner(System.in);
10         Set<Integer>set=new TreeSet<Integer>();
11         while(scan.hasNext()){
12             String str1=scan.nextLine();
13             String result1[]=str1.split(" ");
14             int n=Integer.parseInt(result1[0]);
15             int m=Integer.parseInt(result1[1]);
16             String str2=scan.nextLine();
17             String result2[]=str2.split(" ");
18             for(int i=0;i<result2.length;i++){
19                 set.add(Integer.parseInt(result2[i]));
20             }
21             String str3=scan.nextLine();
22             String result3[]=str3.split(" ");
23             for(int x=0;x<result3.length;x++){
24                 set.add(Integer.parseInt(result3[x]));
25             }
26             Iterator<Integer>iter=set.iterator();
27             StringBuffer sub=new StringBuffer();
28             while(iter.hasNext()){
29                 sub.append(iter.next()).append(" ");
30             }
31             sub.delete(sub.length()-1, sub.length());
32             System.out.println(sub.toString());
33         }
34  
35     }
36  
37 }
View Code

大神方案二

技術分享
 1 import java.io.BufferedReader;
 2 import java.io.InputStreamReader;
 3 import java.util.Iterator;
 4 import java.util.Set;
 5 import java.util.TreeSet;
 6     
 7 public class Main {
 8     public static void main(String[] args) throws Exception {
 9         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
10         String line = null;
11         while((line = br.readLine()) != null){
12             String[] s = line.split(" ");
13             int n = Integer.parseInt(s[0]);
14             int m = Integer.parseInt(s[1]);
15             Set<Integer> set = new TreeSet<Integer>();
16             line = br.readLine();
17             String[] s1 = line.split(" ");
18             for(int i=0;i<n;i++){
19                 set.add(Integer.parseInt(s1[i]));
20             }
21             line = br.readLine();
22             String[] s2 = line.split(" ");
23             for(int i=0;i<m;i++){
24                 set.add(Integer.parseInt(s2[i]));
25             }
26             Iterator<Integer> it = set.iterator();
27             StringBuffer sb = new StringBuffer();
28             while(it.hasNext()){
29                 sb.append(it.next());
30                 sb.append(" ");
31             }
32             sb.delete(sb.length()-1, sb.length());
33             System.out.println(sb.toString());
34         }
35     }
36 }
View Code

在線編程筆試練習2(京東)