sicily 1237. Paint Mix

来源:互联网 发布:淘宝发空包什么意思 编辑:程序博客网 时间:2024/04/29 17:19

1237. Paint Mix

Constraints

Time Limit: 7 secs, Memory Limit: 32 MB

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 that1 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 12 1 43 20 70 0 0

Sample Output

1453866 

题目分析

来回混合原料直到两个桶内,白原料和黑原料的比值与目标值误差小于0.00001

直接模拟,注意变量的改变


#include <stdio.h>#define ESP 0.00001#define fabs(x) (((x)<0)?(-(x)):((x)))int main(){  int b, w, c;  while (scanf("%d%d%d", &b, &w, &c)) {    if (b == 0 && w == 0 && c == 0)      break;    double target = 1.0 * b / w;    double binb = 16.0*b, winb = 0.0;    double bb = binb + winb;    double binw = 0.0, winw = 16.0*w;    double ww = binw + winw;    double takeb, takew;    int count = 0;    while (fabs(binb/winb - target) >= ESP || fabs(binw/winw - target) >= ESP) {      takeb = c * binb / bb;      takew = c * winb / bb;      binb -= takeb;      winb -= takew;      binw += takeb;      winw += takew;      ww = binw + winw;      takeb = c * binw / ww;      takew = c * winw / ww;      binw -= takeb;      winw -= takew;      binb += takeb;      winb += takew;      bb = binb + winb;      count++;    }    printf("%d\n", count);  }}



0 0
原创粉丝点击