-
Notifications
You must be signed in to change notification settings - Fork 0
/
taum_and_bday.c
80 lines (62 loc) · 1.77 KB
/
taum_and_bday.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
Taum and Bday Problem on Hacker Rank
Problem Link: https://www.hackerrank.com/challenges/taum-and-bday/problem
Author: Shyam Kumar
*/
#include <stdio.h>
unsigned long int taum_and_bday( unsigned long int b, unsigned long int w,
unsigned long int bc, unsigned long int wc,
unsigned long int z
) {
unsigned long int total_cost;
/* If the cost of each black gift is less than cost of each
white gift */
if( bc < wc ) {
/* First, we add total cost of black gift to the total cost */
total_cost = b * bc;
/* Check if the conversion cost is less than total cost of white gift
If it is, then add conversion cost to total cost */
if( bc+z < wc )
total_cost += ( bc+z ) * w;
/* If the conversion cost is more than total cost of white gift
then add the total cost of white gifts */
else
total_cost += ( w*wc );
}
/* If the cost of each white gift is less than cost of each
black gift */
else if( wc < bc ) {
total_cost = w * wc;
if( wc+z < bc )
total_cost += ( wc+z ) * b;
else
total_cost += ( b*bc );
}
/* If both the costs are equal */
else {
total_cost = w*wc + b*bc;
}
return total_cost;
}
int main(int argc, char const *argv[])
{
unsigned long int w, b, bc, wc, z, cost;
int i, t;
printf("\nEnter the number of test cases:");
scanf("%d",&t);
for( i = 0; i < t; i++ ) {
printf("\nEnter the number of black gifts: ");
scanf("%lu",&b);
printf("\nEnter the number of white gifts: ");
scanf("%lu",&w);
printf("\nEnter the cost of each black gift: ");
scanf("%lu",&bc);
printf("\nEnter the cost of each white gift: ");
scanf("%lu",&wc);
printf("\nEnter the conversion cost: ");
scanf("%lu",&z);
cost = taum_and_bday( b, w, bc, wc, z );
printf("Cost = %lu",cost);
}
return 0;
}