标签:i++ array tin algo-53 get ons oid 顺序 info
2 3 1 3 -5 -2 4 1 5 1 2 3 4 5 1 0 1 0 1
样例输出
-25
6
import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (0 != t--) { List<Integer> l1 = new ArrayList<Integer>(); List<Integer> l2 = new ArrayList<Integer>(); int n = sc.nextInt(); for (int i = 0; i < n; i++) { l1.add(sc.nextInt()); } for (int i = 0; i < n; i++) { l2.add(sc.nextInt()); } Collections.sort(l1); Collections.sort(l2); int sum = 0; for (int i = 0; i < n; i++) { sum += l1.get(i) * l2.get(n - i - 1); } System.out.println(sum); } sc.close(); } }
思路:将题目中给出的两组数据排序,大的和小的相乘。遍历一遍输出结果即可。
标签:i++ array tin algo-53 get ons oid 顺序 info
原文地址:http://www.cnblogs.com/huanhuan10/p/6387620.html