OJ Platform--shaping array merging

Source: Internet
Author: User



Title Description:



Merges two integer arrays in ascending order and filters out duplicate array elements



Input:



Enter a description, entered in the following order:
1 Enter the number of the first array
2 Enter a numeric value for the first array
3 Enter the number of the second array
4 Enter a value for the second array



Output:



Array after the output is merged



Input Sample:



3 1 2 5 4-1 0 3 2



Sample output:



-101235



Ideas:



First, merge the two arrays into one, then sort the entire array, and finally reject the duplicated data and output


1 ImportJava.util.Scanner;2 3  Public classArraymerge {4 5  Public Static voidMain (string[] args) {6 //Input , enter two arrays of data according to the input criteria7Scanner cin =NewScanner (system.in); 8 intLen1 =cin.nextint ();9 if(Len1 < 1){TenSystem.exit (-1); One  } A int[] arr1 =New int[len1]; -  for(inti = 0; i < len1; i++){ -Arr1[i] =cin.nextint (); the  } - intLen2 =cin.nextint (); - if(Len2 < 1){ -System.exit (-1); +  } - int[] arr2 =New int[len2]; +  for(inti = 0; i < len2; i++){ AArr2[i] =cin.nextint (); at  }  - cin.close (); -  - System.out.print (INTARRAYMERGEASC (ARR1,ARR2));  -  } -  in /** - * Merge and then store the output in a string to return to  * @paramarr1 +  * @paramarr2 -  * @return the */ * Private StaticString INTARRAYMERGEASC (int[] arr1,int[] arr2) { $ //merges two arrays into a larger arrayPanax Notoginseng int[] Total =New int[Arr1.length +Arr2.length]; -  for(inti = 0; i < arr1.length; i++){ theTotal[i] =Arr1[i]; +  } A  for(inti = 0; i < arr2.length; i++){ theTotal[i+arr1.length] =Arr2[i]; +  } - //to sort a merged array $ sort (total); $ //saved in the result in a string -String res = total[0]+ "" ; - intLast = Total[0] ; the  for(inti = 1; i < total.length; i++){ - //not the same is added to the string, the same is the next directWuyi if(Total[i]! =Last ) { theLast =Total[i]; -Res + = Total[i] + "" ; Wu  } -  } About  $ returnRes; -  -  } -  A /** + * Simple selection sorting algorithm for sorting arrays the  * @paramarr - */ $ Private Static voidSortint[] arr) { the intmin; the intindex; the  for(inti = 0; i < arr.length-1; i++){ theMin =Arr[i]; -index =i; in  for(intj = i+1; J < Arr.length; J + +){ the if(Min >Arr[j]) { theMin =Arr[j]; Aboutindex =J; the  } the  } the if(Index! =i) { +Arr[index] =Arr[i]; -Arr[i] =min;  the  }Bayi  } the  } the  -}
Code




 OJ Platform--shaping array merging


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.