but i guess its not that simple like the title says :D
ok .. im doing a excercize but im stuck here ..
the excersize sounds like this:
-You are given N coins of value 3 and M coins of value 5.
-Return the smallest sum,
-strictly greater than X that you can achieve by summing up some
-(not necessarily all) of the coins.
If it's not possible to get a number larger than X return -1.
my code:
coin1 is const int == 3;
coin2 is const int == 5;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
int CoinAddition(int n, int m, int x){
int fr = 0; // function result
if((n*coin1)+(m*coin2) <= x){
return -1;
}else{
while(fr <= x){
if(n > 0){
if(coin2 * 2 >= x && m > 1){
fr += coin2 * 2;
m -= 2;
}else{
fr += coin1;
n -= 1;
}
}else if(m > 0){
fr += coin2;
m -= 1;
}
else break;
} // end of while
return fr;
}
} // end of functionj
| |
my problem:
the problem only works as you can see first by adding 3 ..
so if i have 2 coins of value 3 and 3 coins of value 5 and i put value to achive greater than 14 .. i get result of 16 instead of 15..
the program works like it should right now but..
somehow i need to make the program to choose which coin to add to the sum .. but how ..
because 2x3 = 6 + 5 = 11 + 5 = 16;
what i want to achive is 14 so its easier .. 3 x 5 = 15
15 > 14 and < 16
16 > 14 and > 15
so ... how?