10 3 4 0.1 4 0.2 5 0.3 0 0
44.0%HintYou should use printf("%%") to print a '%'.
题目要求最少获得一份补助的意思,就是(1 - 一份都不获得的概率),把这个转换好了,题目就好做了。
另外:求的是最小背包,不要习惯用max函数了,初始化的时候为1就可以了。
代码如下:
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; struct node { int v; double w; }c[10011]; double dp[10011]; int v,n; void init() { for (int i = 0 ; i <= v ; i++) dp[i] = 1.0; } double min(double a,double b) { return a < b ? a : b; } int main() { while (~scanf ("%d %d",&v,&n) && (v || n)) { for (int i = 1 ; i <= n ; i++) { scanf ("%d %lf",&c[i].v,&c[i].w); c[i].w = 1.0 - c[i].w; } init(); for (int i = 1 ; i <= n ; i++) { for (int j = v ; j >= c[i].v ; j--) { dp[j] = min (dp[j] , dp[j-c[i].v] * c[i].w); } } dp[v] = 1.0 - dp[v]; dp[v] *= 100.0; printf ("%.1lf%%\n",dp[v]); } return 0; }