B - Buy One Carton of Milk
https://atcoder.jp/contests/abc331/tasks/abc331_b
思路
dfs递归搜索, 按照依此按照三种策略:
6个一打 - cost S
8个一打 - cost M
12个一打 - cost L
递归到叶子节点终止条件为, 总的cost超过预算N,记录此时花费,更新 mincost
Code
https://atcoder.jp/contests/abc331/submissions/48127455
int N, S, M, L; int mincost = INT_MAX; void dfs(int sum, int cost){ if (sum >= N){ if (cost < mincost){ mincost = cost; } return; } dfs(sum+6, cost+S); dfs(sum+8, cost+M); dfs(sum+12, cost+L); } int main() { cin >> N >> S >> M >> L; dfs(0, 0); cout << mincost << endl; return 0; }
标签:Buy,int,sum,mincost,dfs,cost,Carton,Milk From: https://www.cnblogs.com/lightsong/p/17876170.html