12502 - Three Families
Three families share a garden. They usually clean the garden together at the end of each week, but lastweek, family C was on holiday, so family A spent 5 hours, family B spent 4 hours and had everythingdone. After coming back, family C is willing to pay $90 to the other two families. How much shouldfamily A get? You may assume both families were cleaning at the same speed.
$90/(5+4)*5=$50? No no no. Think hard. The correct answer is $60. When you figured out why,answer the following question: If family A and B spent x and y hours respectively, and family C paid$z, how much should family A get? It is guaranteed that both families should get non-negative integerdollars.
WARNING: Try to avoid floating-point numbers. If you really need to, be careful!
Input
The first line contains an integer T (T100), the number of test cases. Each test case contains threeintegersx, y, z (1x,y10,1z1000).Output
For each test case, print an integer, representing the amount of dollars that family A should get.Sample Input
2
5 4 90
8 4 123
Sample Output
60
12
解题思路:该是一个数学题,只要推算出数学公式即可解得。该题在2012年湖南省第八届大学生程序设计大赛中出现过,但是那时木有提示小数的问题,WA了好几次才对。
1、3家人,第3家人只为自己该做工作的天数付钱,如果A家庭做的天数大于工作的总天数(家庭A+家庭B)/3时,才能得到第3个家庭付的钱。if(x>(x+y)/3)
2、由于最后得到的钱为整数,要是中途有除法,我们所有的变量都是整形的,会造成数据遗失(小数部分木有了)。所以,在处理时,要把除法提到最后面,保证数据的可靠性。sum=(3*x-(x+y))*z/(x+y);
#include<stdio.h>
int main()
{
int x,y,z;
int t,sum,k;
scanf("%d",&t);
while(t--)
{
sum=0;
scanf("%d%d%d",&x,&y,&z);
if(x>(x+y)/3)
sum=(3*x-(x+y))*z/(x+y);
printf("%d\n",sum);
}
return 0;
}