package qunar; import java.util.Scanner; public class Main2 { static int rel =9999; public static void main(String[] args) { // TODO Auto-generated method stub Scanner in = new Scanner(System.in); String s; while(in.hasNext()){ s = in.nextLine(); rel = 9999; gs(s); if(rel != 9999){ System.out.println(rel); }else{ System.out.println("-1"); } } } public static void gs(String s){ String[] ar = s.split(" "); int n = ar.length; int[] arr = new int[n]; for(int i=0;i<n;i++){ arr[i] = Integer.valueOf(ar[i]); } dp(arr,n-2,arr[n-1],0); } public static void dp(int[] arr,int now,int money,int day){ if(money < 0){ return; } if(money == 0){ if(day < rel){ rel = day; return ; } } if(money%arr[now] == 0){ if(money/arr[now] < rel){ rel = money/arr[now]+day; return; } }else{ for(int i=now;i>=0;i--){ if(money>=arr[i]){ dp(arr,i,money-arr[i],day+1); } } } } } 有一些多余的地方
点赞 2
牛客网
牛客企业服务