码迷,mamicode.com
首页 > 其他好文 > 详细

Taum and B'day

时间:2015-10-29 09:36:16      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:

https://www.hackerrank.com/challenges/taum-and-bday

 

Problem Statement

Taum is planning to celebrate the birthday of his friend, Diksha. There are two types of gifts that Diksha wants from Taum: one is black and the other is white. To make her happy, Taum has to buy B number of black gifts and W number of white gifts.

  • The cost of each black gift is X units.
  • The cost of every white gift is Y units.
  • The cost of converting each black gift into white gift or vice versa is Z units.

Help Taum by deducing the minimum amount he needs to spend on Diksha‘s gifts.

Input Format

The first line will contain an integer T which will be the number of test cases.
There will be T pairs of lines. The first line of each test case will contain the values of integersB and W. Another line of each test case will contain the values of integers XY, and Z.

Constraints 
1T10 
0X,Y,Z,B,W109

Output Format

T lines, each containing an integer: the minimum amount of units Taum needs to spend on gifts.

 

 

Solution:

Initial thought was using a loop to test number of black that products the minimum cost(j*black+(sum-j)*white+(diff*z))

But actually, we only need to decide the minimum cost of black and white, the numbers stays the same.No loop is neccesary

 public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        int num=in.nextInt();
        for(int i=0;i<num;i++){
            long a=in.nextInt();
            long b=in.nextInt();
            long ac=in.nextInt();
            long bc=in.nextInt();
            long cc=in.nextInt();
            long black=Math.min(ac,bc+cc);
            long white=Math.min(bc,ac+cc);
            long v=black*a+white*b;
            System.out.println(v);
        }
    }

 

 

 
 

Taum and B'day

标签:

原文地址:http://www.cnblogs.com/fifi043/p/4919328.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!