编辑代码

public class SilverCoins {  
    public static boolean canCollect(int[] coins, int target) {  
        int n = coins.length;  
        boolean[][] dp = new boolean[n + 1][target + 1];  
        for (int i = 0; i <= n; i++) {  
            dp[i][0] = true;  
        }  
        for (int i = 1; i <= n; i++) {  
            for (int j = 1; j <= target; j++) {  
                if (coins[i - 1] <= j) {  
                    dp[i][j] = dp[i - 1][j] || dp[i - 1][j - coins[i - 1]];  
                } else {  
                    dp[i][j] = dp[i - 1][j];  
                }  
            }  
        }  
        return dp[n][target];  
    }  

    public static void main(String[] args) {  
        // 测试用例1  
        int[] coins1 = {2, 4, 6};  
        int target1 = 5;  
        boolean result1 = SilverCoins.canCollect(coins1, target1);  
        System.out.println("测试用例1:是否可以收集到总价值为" + target1 + "的银币:" + result1);  
  
        // 测试用例2  
        int[] coins2 = {2, 4, 6, 8};  
        int target2 = 10;  
        boolean result2 = SilverCoins.canCollect(coins2, target2);  
        System.out.println("测试用例2:是否可以收集到总价值为" + target2 + "的银币:" + result2);  
  
        // 测试用例3  
        int[] coins3 = {1, 3, 5};  
        int target3 = 7;  
        boolean result3 = SilverCoins.canCollect(coins3, target3);  
        System.out.println("测试用例3:是否可以收集到总价值为" + target3 + "的银币:" + result3);  
  
        // 测试用例4  
        int[] coins4 = {1, 2, 5, 9};  
        int target4 = 14;  
        boolean result4 = SilverCoins.canCollect(coins4, target4);  
        System.out.println("测试用例4:是否可以收集到总价值为" + target4 + "的银币:" + result4);  
    }

}