天天看點

NYOJ-927(搜尋)-題目-----------------------------The partial sum problem

package org.acm.search;

/*http://acm.nyist.net/JudgeOnline/problem.php?pid=927*/

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;

public class Search_927 {

	private static long n, k;
	private static long data[] = new long[20];
	private static long sum;

	public static void main(String[] args) {

		Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(System.in)));

		while (sc.hasNext()) {

			n = sc.nextLong();

			sum = 0;
			for (int i = 0; i < n; i++) {
				data[i] = sc.nextLong();
				sum += data[i];
			}

			k = sc.nextLong();
			if (sum < k)
				System.out.println("Sorry,I can't!");
			else {
				if (dfs(0, 0))
					System.out.println("Of course,I can!");
				else
					System.out.println("Sorry,I can't!");
			}
		}
		sc.close();
	}

	private static boolean dfs(int i, long s) {

		if (s == k)
			return true;

		if (i >= n)
			return false;

		if (dfs(i + 1, s + data[i]))
			return true;
		else
			return dfs(i + 1, s);

	}
}