题目:http://acm.hdu.edu.cn/showproblem.php?pid=1203
Speakless有n万元,申请m个学校,每个学校有花费的金额和入学的可能性,求他最少进入一个学校的可能性。
Sample Input
10 3 4 0.1 4 0.2 5 0.3 0 0
Sample Output
44.0%
#include<iostream>
#include<string.h>
#include<algorithm>
#include<stdio.h>
using namespace std;
const int maxn = 10000 + 5;
int main()
{
double dp[maxn], val[maxn],ans;
int w[maxn];
int n,m,i, j;//n万元m个
while (cin >> n >> m&&(n+m)!=0)
{
for (j = 0; j <= n; j++)
dp[j] = 1;
for (i = 0; i < m; i++)
{
cin >> w[i] >> val[i];
val[i] = 1 - val[i];//得不到offer的可能性
}
for (i = 0; i < m; i++)
{
for (j = n; j >= w[i]; j--)//要n开始,确保每种物品最多取一次
{
dp[j] = min(dp[j], dp[j - w[i]] * val[i]);
}
}
ans = (1 - dp[n])*100;
printf("%.1f%%\n", ans);
}
return 0;
}