Close Enough Computations
题目连接:
Descriptionww.co
The nutritional food label has become ubiquitous. A sample label is shown to the right. On the label the number of calories and the number of grams of fat, carbohydrate, and protein are given as integers.
But carefully reading the label may cause the consumer to notice some inconsistencies. A gram of fat has 9 calories, a gram of carbohydrate has 4 calories, and a gram of protein has 4 calories. Consider the label to the right. A simple computation of the number of calories would indicate that the food should contain 129 + 314 + 5*4 or 252 calories, but the label indicates it has 250 calories.
While sometimes the difference in calories is due to other circumstances (such as the presence of alcohol or soluble fiber), this problem will consider only the possibility of round-off error. This food actually has 12.1 grams of fat (yielding 108.9 calories), 30.6 grams of carbohydrate (122.4 calories), 4.7 grams of protein (18.8 calories), so it does in fact have 250 calories (actually 250.1 calories).
Write a program that will determine if values for a nutritional label are consistent, that is, if there is a way the true values for the grams of nutrients can be rounded to the shown values and yield the number of calories shown.
You should assume that standard rounding rules apply; that is any value less than 0.5 rounds down and those 0.5 or over round up.
Input
The input will contain one or more sets of data about potential labels. Each data set will consist of 4 non-negative integers, separated by one or more blanks, on a single line. The integers represent the number of calories, the number of grams of fat, the number of grams of carbohydrates, and the number of grams of protein, in that order. The number of calories will not exceed 10000, and the number of grams of any component will not exceed 1000.
End of input is indicated by a line containing 4 zeroes. This line should not be processed.
Output
For each data set, print "yes" or "no" on its own line, indicating whether the given rounded values of the three nutrients can yield the given number of calories.
Sample Input
250 12 31 5
250 13 31 5
122 10 10 0
0 0 0 0
Sample Output
yes
no
no
Hint
题意
有一个包装袋上面写着含a卡路里,含有b克XX,c克YY,d克ZZ
每克b含有9卡路里,每克c和d含有4卡路里
但是包装上的数字都是四舍五入的,问你有没有可能这个包装是真实的,即实际卡路里四舍五入之后为a
题解:
显然,只要在[a-0.5,a+0.5)这个范围就好了,我们算出能够得到的最多卡路里,和最少卡路里区间,看看这两个有没有交集就好了
代码
#include<bits/stdc++.h>
using namespace std;
int main()
{
double a,b,c,d;
while(scanf("%lf%lf%lf%lf",&a,&b,&c,&d)!=EOF)
{
if(a==0&&b==0&&c==0&&d==0)break;
int flag = 1;
double l = a-0.5,r = a+0.5-0.000001;
double l1 = max((b-0.5),0.0)*9+max((c-0.5),0.0)*4+max((d-0.5),0.0)*4;
double r1 = (b+0.5)*9.0+(c+0.5)*4+(d+0.5)*4-0.000001;
if(r<l1||l>r1)flag=0;
if(flag)printf("yes
");
else printf("no
");
}
}