poj 2715 Paint Mix

Paint Mix
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 1462 Accepted: 461

Description

You are given two large pails. One of them (known as the black pail) contains B gallons of black paint. The other one (known as the white pail) contains W gallons of white paint. You will go through a number of iterations of pouring paint first from the black pail into the white pail, then from the white pail into the black pail. More specifically, in each iteration you first pour C cups of paint from the black pail into the white pail (and thoroughly mix the paint in the white pail), then pour C cups of paint from the white pail back into the black pail (and thoroughly mix the paint in the black pail). B, W, and C are positive integers; each of B and W is less than or equal to 50, and C < 16 * B (recall that 1 gallon equals 16 cups). The white pail's capacity is at least B+W.

As you perform many successive iterations, the ratio of black paint to white paint in each pail will approach B/W. Although these ratios will never actually be equal to B/W one can ask: how many iterations are needed to make sure that the black-to-white paint ratio in each of the two pails differs from B/W by less than a certain tolerance. We define the tolerance to be 0.00001.

Input

The input consists of a number of lines. Each line contains input for one instance of the problem: three positive integers representing the values for B, W, and C, as described above. The input is terminated with a line where B = W = C = 0.

Output

Print one line of output for each instance. Each line of output will contain one positive integer: the smallest number of iterations required such that the black-to-white paint ratio in each of the two pails differs from B/W by less than the tolerance value.

Sample Input

2 1 1
2 1 4
3 20 7
0 0 0

Sample Output

145
38
66

Hint

Huge input and output data,scanf and printf are recommended.
#include<iostream>
#include<cmath>

using namespace std;

#define EPS 0.00001

int main()
{
double B, W, C;
while(scanf("%lf%lf%lf", &B, &W, &C) && (B + W + C))
{
B *= 16, W *= 16;
double ans = B / W;
double raW = 0, raB = 1;
int sum = 0;
while(fabs(raW / (1 - raW) - ans) > EPS || fabs(raB / (1 - raB) - ans) > EPS)
{
raW = 1 - (W * (1 - raW) + C * (1 - raB)) / (C + W);
raB = ((B - C) * raB + C * raW) / B;
sum ++;
}
printf("%d\n", sum);
}
}

转载于:https://www.cnblogs.com/w0w0/archive/2011/11/22/2258647.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值