In this HackerRank Taum and B’day problem, you need to determine the minimum cost of Disksha’s gifts.
Problem solution in Python programming.
tcs = int(input()) for i in range(tcs): b, w = map(int, input().split(' ')) x, y, z = map(int, input().split(' ')) if x + z < y: print(b*x+w*(x+z)) elif y + z < x: print(w*y+b*(y+z)) else: print(b*x+w*y)
Problem solution in Java Programming.
import java.io.*; import java.util.*; public class Solution { private static long cost( long B,long W,long X,long Y,long Z ) { long cost = 0; if( X < Y ) { cost = cost + ( B * X ); if( Y < Z + X ) { cost = cost + ( W * Y ); } else { cost = cost + ( W * ( Z + X ) ); } } else { cost = cost + ( W * Y ); if( X < Z + Y ) { cost = cost + ( B * X ); } else { cost = cost + ( B * ( Z + Y ) ); } } return cost; } public static void main(String[] args) { Scanner scan = new Scanner( System.in ); int testCases = scan.nextInt(); for( int i = 0;i < testCases;i++ ) { System.out.println( cost( scan.nextLong(),scan.nextLong(),scan.nextLong(),scan.nextLong(),scan.nextLong() ) ); } } }
Problem solution in C++ programming.
#include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> using namespace std; int main() { int cases; scanf("%d", &cases); while (cases--) { int B, W, X, Y, Z; scanf("%d %d %d %d %d", &B, &W, &X, &Y, &Z); long long res = (long long)B * X + (long long)W * Y; res = min(res, (long long)B * X + (long long)W * (X + Z)); res = min(res, (long long)B * (Y + Z) + (long long)W * Y); printf("%lldn", res); } return 0; }
Problem solution in C programming.
#include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> int main() { unsigned long long int B, W, X, Y, Z, black, white, BtoW, WtoB, sum, T; scanf("%llu", &T); while(T--) { scanf("%llu", &B); scanf("%llu", &W); scanf("%llu", &X); scanf("%llu", &Y); scanf("%llu", &Z); black = B * X; white = W * Y; BtoW = W * (Z + X); WtoB = B * (Z + Y); //no transfer sum = 0; sum = black + white; if ( black + BtoW < sum) sum = black + BtoW; if ( white + WtoB < sum) sum = white + WtoB; printf("%llun", sum); } }
Problem solution in JavaScript programming.
function processData(input) { var testCases = input.split('n'); testCases.shift(); while (testCases.length) { var presents = testCases.shift().split(' ').map(function(n) { return new BigNum(n); }); var prices = testCases.shift().split(' ').map(function(n) { return parseInt(n, 10); }); var blackPresentCost = new BigNum(Math.min(prices[0], prices[1] + prices[2]) + "").multiplyBy(presents[0]); var whitePresentCost = new BigNum(Math.min(prices[1], prices[0] + prices[2]) + "").multiplyBy(presents[1]); console.log(blackPresentCost.add(whitePresentCost).toString()); } } var BigNum = function(num) { if (typeof num === 'string') { this.parts = []; while (num.length) { this.parts.push(parseInt(num.slice(-5),10)); num = num.substring(0,num.length-5) } } else { this.parts = num; } } BigNum.prototype.multiplyBy = function(bigNum) { var newParts = []; for (var i = 0, l1 = this.parts.length; i < l1; ++i) { for (var j = 0, l2 = bigNum.parts.length; j < l2; ++j) { var newPartIndex = i + j; newParts[newPartIndex] = this.parts[i] * bigNum.parts[j] + (newParts[newPartIndex] || 0); } } this._fixOverflows(newParts); return new BigNum(newParts); }; BigNum.prototype.add = function(bigNum) { var newParts = []; var length = Math.max(this.parts.length, bigNum.parts.length); for (var i = 0; i < length; ++i) { newParts[i] = (this.parts[i] || 0) + (bigNum.parts[i] || 0); } this._fixOverflows(newParts); return new BigNum(newParts); } BigNum.prototype._fixOverflows = function(parts) { for (var k = 0; k < parts.length; ++k) { var currentPart = parts[k].toString(); if (currentPart.length > 5) { var overflowLength = currentPart.length - 5; var overflow = parseInt(currentPart.substr(0, overflowLength), 10); parts[k] = parseInt(currentPart.substr(overflowLength), 10) parts[k + 1] = overflow + (parts[k + 1] || 0); } } } BigNum.prototype.toString = function() { var fullNumber = this.parts.map(function(num) { num = num.toString(); paddingAmount = 5 - num.length; for (var i = 0; i < paddingAmount; ++i) { num = '0' + num; } return num }).reverse().join(''); while( fullNumber.charAt(0) === '0' && fullNumber.length !== 1) { fullNumber = fullNumber.substring(1); } return fullNumber; }; process.stdin.resume(); process.stdin.setEncoding("ascii"); _input = ""; process.stdin.on("data", function (input) { _input += input; }); process.stdin.on("end", function () { processData(_input); });