您的位置:首页 > 其它

UVA 147 Dollars 刀了(完全背包,精度问题)

2015-05-09 14:52 246 查看
题意:一样是求钱的转换方案数,但是这次单位下降到分,但给的是元为单位的,所以是浮点的,但是固定有两位小数。

思路:数据都放大100倍来计算,去除精度问题,转成整型时要注意精度。即使给的是0.02,乘以100后的结果不一定是2,而是2左右,所以再加上一个很小的数再转即可,比如0.0001;

#include <iostream>
#include <cstdio>
#define LL long long
using namespace std;
const int  N=30002;
int coin[]={5, 10, 20, 50, 100, 200, 500, 1000, 2000, 5000, 10000};
LL dp[N+10]={1};
void cal()
{
for(int i=0; i<11; i++)
for(int j=0; j+coin[i]<=N; j++ )
dp[j+coin[i]]+=dp[j];
}
int main()
{
//freopen("input.txt", "r", stdin);
cal();
double n;
while(scanf("%lf",&n),n!=0.00)
printf("%6.2lf%17lld\n", n, dp[int(n*100+0.001)] );//重点在精度
return 0;
}


AC代码
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: