Title Description: (dp problem )
small easy to go to the nearby shops to buy apples, treacherous traders used the bundle, only 6 bags per bag and 8 bags per bag (packaging is not detachable). But small easy now just want to buy exactly n apples, small easy to buy as little as possible to carry the number of bags. If you can't buy exactly n apples, small easy will not be purchased. Input Description:Enter an integer n, which means it's easy to buy n (1≤n≤100) ApplesOutput Description:output An integer indicating the minimum number of bags to buy, and if you can't buy exactly n apples then output-1 Input Example: -Output Example:3idea: DP problem. First Dp[i] = min (dp[i],dp[i-a[j]+1). Enter the total number of apples, the DP is initially sum, so that the last dp[sum] = sum does not have the result of just sum, otherwise input dp[sum]
1#include <iostream>2 using namespacestd;3 //Dp[i] = min (dp[i],dp[i-a[j]+1)4 5 intMain ()6 {7 intsum;8 while(cin>>sum)9 {Ten inta[2] = {6,8}; One intdp[ -]; Adp[0] =0;//is also the key point - for(intI=1;i< -; i++) - { theDp[i] =sum; - } - for(intI=0; i<=sum;i++) - { + for(intj=0;j<2; j + +) - { + if(I>=a[j] && Dp[i-a[j]] +1<Dp[i]) A { atDp[i] = dp[i-a[j]]+1; - } - } - } - if(Dp[sum] = =sum) -cout<<-1<<Endl; in Else -cout<<dp[sum]<<Endl; to return 0; + } -}
2017 NetEase Autumn recruit--7, buy apples