{ "question_id": 7420132, "question_title": "动态规划-硬币重量最轻问题", "question_content": "

设有n种不同面值的硬币,第i种硬币的币值是Vi(其中V1=1),重量是Wi,i=1,2,...n且现在购买某种总币值为y的商品,需要用这些硬币付款,如果每种钱币使用的个数不限,那么如何选择付款的方法使得付出钱币的总重量最轻?使用动态规划设计策略设计一个求解该问题的算法。假设问题的输入实例是:

V1=1, V2=4, V3=6, V4=8

W1=1, W2=2,W3=4,W4=6

Y=12

要求输出优化函数表和标记函数表、以及硬币支付方式。

\n", "difficulty": "困难", "answer_id": 53378192, "answer_content": "
\n#include<stdio.h>\nvoid strcpy(int *a, int *b, int Y){\n    for(int i=0;i<=Y;i++) *(a+i) = *(b+i);\n}\nvoid solve(){\n        int n; scanf("%d",&n);\n        int type[n], weight[n], Y, i, j, k;\n        for(i=0;i<n;i++) scanf("%d",&type[i]);\n        for(i=0;i<n;i++) scanf("%d",&weight[i]);\n        scanf("%d",&Y);\n        int Min[Y+1], Min_Path[Y+1], path[n][Y+1];\n        for(i=0;i<=Y;i++) Min[i] = 9999;\n        Min[0] = 0;\n        printf("\\n");\n        for(j=0;j<n;j++){\n            for(i=type[j]; i<=Y; i++)\n                if(Min[i] > Min[i-type[j]]+weight[j]){\n                    Min_Path[i] = type[j];\n                    Min[i] = Min[i-type[j]]+weight[j];\n                }\n            for(k=1;k<=Y;k++) printf("%-3d",Min[k]);\n            printf("\\n");\n            strcpy(path[j],Min_Path,Y);\n        }\n\n        printf("\\n");\n        for(i=0;i<n;i++){\n            for(j=1;j<=Y;j++)\n                printf("%-3d",path[i][j]);\n            printf("\\n");\n        }\n\n        int y=Y;\n        printf("\\n支付方式:");\n        while (y){\n            printf("%d ",Min_Path[y]);\n            y -= Min_Path[y];\n        }\n        printf("\\n总重量:%d\\n",Min[Y]);\n}\nint main(){\n    solve();\n    return 1;\n}\n/*\n4\n1 4 6 8\n1 3 2 6\n12\n */
\n\n

\"\"

\n\n

 

", "tag_name": "c语言", "cpp": "#include\nvoid strcpy(int *a, int *b, int Y){\n\tfor(int i=0;i<=Y;i++) *(a+i) = *(b+i);\n}\nvoid solve(){\n\tint n; scanf(\"%d\",&n);\n\tint type[n], weight[n], Y, i, j, k;\n\tfor(i=0;i Min[i-type[j]]+weight[j]){\n\t\t\t\tMin_Path[i] = type[j];\n\t\t\t\tMin[i] = Min[i-type[j]]+weight[j];\n\t\t\t}\n\t\tfor(k=1;k<=Y;k++) printf(\"%-3d\",Min[k]);\n\t\tprintf(\"\\n\");\n\t\tstrcpy(path[j],Min_Path,Y);\n\t}\n\tprintf(\"\\n\");\n\tfor(i=0;i