滚动数组实现1:
#include<stdio.h>
#include<string.h>
#include<math.h>
double my_max(double x,double y)
{
if(x>y)
return x;
else
return y;
}
typedef struct node{
int x;
double f;
}NODE;
NODE que[10005];
double dp[2][10005];
int main()
{
int n,m,i,j;
while(scanf("%d %d",&n,&m)!=EOF)
{
if(m==0&&n==0) break;
memset(dp,0,sizeof(dp));
memset(que,0,sizeof(que));
for(i=1;i<=m;i++)
{
scanf("%d",&que[i].x);
scanf("%lf",&que[i].f);
}
for(i=1;i<=m;i++)
{
for(j=0;j<=n;j++)//j是从0开始,不是从1开始
{
dp[i%2][j]=dp[(i-1)%2][j];
if(que[i].x<=j)
{
double t;
t=1-(1-dp[(i-1)%2][j-que[i].x])*(1-que[i].f);
dp[i%2][j]=my_max(dp[i%2][j],t);
}
}
}
printf("%.1lf%%\n",dp[m%2][n]*100);
}
return 0;
}
滚动数组实现2:
#include<stdio.h>
#include<string.h>
#include<math.h>
double my_max(double x,double y)
{
if(x>y)
return x;
else
return y;
}
typedef struct node{
int x;
double f;
}NODE;
NODE que[10005];
double dp[10005];
int main()
{
int n,m,i,j;
while(scanf("%d %d",&n,&m)!=EOF)
{
if(m==0&&n==0) break;
memset(dp,0,sizeof(dp));
memset(que,0,sizeof(que));
for(i=1;i<=m;i++)
{
scanf("%d",&que[i].x);
scanf("%lf",&que[i].f);
}
for(i=1;i<=m;i++)
{
for(j=n;j>=0;j--)//j是从0开始,不是从1开始
{
if(que[i].x<=j)
{
double t;
t=1-(1-dp[j-que[i].x])*(1-que[i].f);
dp[j]=my_max(dp[j],t);
}
}
}
printf("%.1lf%%\n",dp[n]*100);
}
return 0;
}