标签:
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.
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 X, Y, and Z.
Constraints
1≤T≤10
0≤X,Y,Z,B,W≤109
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); } }
标签:
原文地址:http://www.cnblogs.com/fifi043/p/4919328.html