标签:自己 iter author while 技术分享 位置 logs isp stringbu
每组输入数据分为三行,第一行有两个数字n,m(0 ≤ n,m ≤ 10000),分别表示集合A和集合B的元素个数。后两行分别表示集合A和集合B。每个元素为不超过int范围的整数,每个元素之间有个空格隔开。
针对每组数据输出一行数据,表示合并后的集合,要求从小到大输出,每个元素之间有一个空格隔开,行末无空格。
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 }
大神方案一
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 }
大神方案二
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 }
标签:自己 iter author while 技术分享 位置 logs isp stringbu
原文地址:http://www.cnblogs.com/JLZT1223/p/7449363.html