uva 12502 - Three Families

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 (T$ \le$100), the number of test cases. Each test case contains threeintegersx, y, z (1$ \le$x,y$ \le$10,1$ \le$z$ \le$1000).

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;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值