第二题:暴力,AC,JAVA import java.util.Scanner; public class Main2 { public static void main(String[] args){ Scanner sc =new Scanner(System.in); int n = sc.nextInt(); int[] list = new int[n]; for (int i = 0; i < n; i++) list[i] = sc.nextInt(); int tar = sc.nextInt(); System.out.println(count(list,0,0,tar, 0)); } public static boolean count(int[] list, int po, int sum, int tar, int n){ if (po >= list.length) return false; if ((n > 0 && sum == tar) || sum+list[po] == tar) return true; return count(list, po + 1, sum , tar, n) || count(list, po + 1, sum + list[po], tar, n+1); } }
点赞 评论

相关推荐

头像
04-26 15:00
已编辑
算法工程师
点赞 评论 收藏
转发
头像
不愿透露姓名的神秘牛友
03-13 14:57
点赞 评论 收藏
转发
牛客网
牛客企业服务