hdu 4803 Poor Warehouse Keeper 数学+贪心

本文探讨了使用C++解决特定数学问题时遇到的精度问题,并提供了一种通过不断逼近的方法来寻找答案的实现思路。该算法适用于处理与比率有关的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

不知道是不是精度问题,代码交到c++wa,交g++过

总体是很简单的,不断去逼近(y+1-eps)/x

#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <queue>
#include <map>
using namespace std;
#define INF 1e9
#define maxn
#define rep(i,x,y) for(int i=x;i<=y;i++)
#define mset(x) memset(x,0,sizeof(x))
#define eps 1e-9

double x, y;
int ans;

int main(){
//	freopen("a.txt","r",stdin);
//	freopen(".out","w",stdout);
	while(~scanf("%lf%lf", &x, &y)){
		if(x>y){
			puts("-1");
			continue;
		}

		ans=x-1;
		double tmp = (y+1.0-eps)/x;
		double y1 = 1.0;
		rep(x1,1,(int)x){
			int add = (double)x1 * tmp - y1;
			y1 += add;
			y1 = y1 * (double)(x1+1) / (double)x1;
			ans += add;
		}
		printf("%d\n", ans);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值