Problem-solving ideas: Because food can be casually eaten, so is a complete backpack, calorie represents consumption, happiness represents value, set of formula can be done.
Problem Description for foodies, the happiest thing about the new Year is to eat, not one! But for girls, calories are predators. Senior beauty is familiar with the "fat to fall like a mountain, fat to go like a ladder," the truth, so she hoped you can help develop a recipe, can make her eat happy at the same time, will not make too many predators.
Of course, to make your recipes easier, the waterfall gives you a list of daily foods that describe the level of happiness that every food she wants to eat and the amount of calories it will increase. Input inputs contain multiple sets of test cases. Each set of data begins with an integer n, which indicates that the daily food list has n foods.
Next n lines, two integers a and b per line, where a indicates that the food can give the happiness value of the waterfall (the greater the value, the happier it is), and B is the amount of calories that the food will absorb in a waterfall. The last is an integer m, which means that the calories absorbed by the waterfall can not exceed m per day.
[Technical specification]1.1 <= n <= 100 2. 0 <= A, b <= 100000 3. 1 <= m <= 100000 output for each list, outputs an integer, which is the maximum happiness that can be achieved while the calorie uptake is met. Sample Input33 PNs 91051 310-Output1020
#include <stdio.h> #include <string.h>int f[100010],a[100010],b[100010];int max (int a,int b) { if (a >B) return A; else return b;} int main () { int n,i,v,m; while (scanf ("%d", &n)!=eof) { memset (f,0,sizeof (f)); for (i=1;i<=n;i++) scanf ("%d%d", &a[i],&b[i]); scanf ("%d", &m); for (i=1;i<=n;i++) {for (v=b[i];v<=m;v++) F[v]=max (F[v],f[v-b[i]]+a[i]); } printf ("%d\n", F[m]);} }
Hangzhou Electric 4508 Series story--weight loss kee i "full backpack"