andy6804tw
7/17/2016 - 11:31 PM

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?a=15948 這題直接利用Arrays.sort()排序輸入的陣列然後再依序從小開始選購禮物最後在驗證是否超出預算

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?a=15948

這題直接利用Arrays.sort()排序輸入的陣列然後再依序從小開始選購禮物最後在驗證是否超出預算

import java.util.*;

public class Main {

	public static void main(String[] args) {
		Scanner scn = new Scanner(System.in);
		int n = scn.nextInt();
		while (n-- != 0) {
			int b = scn.nextInt(), t = scn.nextInt(), s = scn.nextInt(), arr[] = new int[s], tot = 0;
			for (int i = 0; i < s; i++)
				arr[i] = scn.nextInt();
			Arrays.sort(arr);
			for (int i = 0; i < t; i++)
				tot += arr[i];
			if (b - tot >= 0)
				System.out.println(tot);
			else
				System.out.println("Impossible");
		}
	}
		/* 
    題目:ITSA Basic 題目35. 買獎品
    作者:1010
    時間:西元 2016 年 7 月 */
}