标签:style class blog c code java
1 import java.util.Arrays; 2 import java.util.Comparator; 3 import java.util.Scanner; 4 5 6 public class Main { 7 8 public static void main(String[] args) { 9 // TODO Auto-generated method stub 10 Scanner cin = new Scanner(System.in); 11 int cas_num = cin.nextInt(); 12 for(int cas_idx = 0; cas_idx < cas_num; cas_idx ++) { 13 int num = cin.nextInt(); 14 Integer[] arr1 = new Integer[num]; 15 Integer[] arr2 = new Integer[num]; 16 for(int i = 0; i < num; i ++) { 17 arr1[i] = cin.nextInt(); 18 } 19 20 for(int i = 0; i < num; i ++) { 21 arr2[i] = cin.nextInt(); 22 } 23 Arrays.sort(arr1); 24 Comparator<Integer> cmp = new MyComparator(); 25 Arrays.sort(arr2, cmp); 26 Arrays.sort(arr2); 27 Integer sum = 0; 28 for(int i = 0; i < num; i ++) { 29 sum += (arr1[i] * arr2[num - i - 1]); 30 } 31 System.out.println(sum); 32 } 33 cin.close(); 34 35 } 36 37 } 38 class MyComparator implements Comparator<Integer>{ 39 40 @Override 41 public int compare(Integer o1, Integer o2) { 42 // TODO Auto-generated method stub 43 if(o1.compareTo(o2) < 0) { 44 return 1; 45 }else if(o1.compareTo(o2) > 0){ 46 return -1; 47 }else { 48 return 0; 49 } 50 } 51 }
Java语言知识:
该算法中用到了Java的API,Arrays.sort(),具体用法见我另一篇博客Java中Array.sort()的几种用法
蓝桥杯软件大赛练习系统——算法训练 最小乘积(基本型),布布扣,bubuko.com
标签:style class blog c code java
原文地址:http://www.cnblogs.com/IT-sky/p/3749767.html