Skip to content

Commit 9157d24

Browse files
author
hrishit biswas
committed
Added Bytelandian Gold coins
1 parent f033ff2 commit 9157d24

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
In Byteland they have a very strange monetary system.
2+
3+
Each Bytelandian gold coin has an integer number written on it. A coin n can be exchanged in a bank into three coins: n/2, n/3 and n/4. But these numbers are all rounded down (the banks have to make a profit).
4+
5+
You can also sell Bytelandian coins for American dollars. The exchange rate is 1:1. But you can not buy Bytelandian coins.
6+
7+
You have one gold coin. What is the maximum amount of American dollars you can get for it?
8+
Input
9+
10+
The input will contain several test cases (not more than 10). Each testcase is a single line with a number n, 0 <= n <= 1 000 000 000. It is the number written on your coin.
11+
Output
12+
13+
For each test case output a single line, containing the maximum amount of American dollars you can make.
14+
15+
Example
16+
17+
Input:
18+
12
19+
2
20+
21+
Output:
22+
13
23+
2
24+
25+
You can change 12 into 6, 4 and 3, and then change these into $6+$4+$3 = $13.
26+
If you try changing the coin 2 into 3 smaller coins, you will get 1, 0 and 0, and later you can get no more than $1 out of them.
27+
It is better just to change the 2 coin directly into $2.

Bytelandian-gold-coins/solution.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
map<int,long long> dp;
4+
long long optcoins(int n){
5+
if(n<12)
6+
return n;
7+
if(dp.count(n))
8+
return dp[n];
9+
dp[n]=optcoins(n/2)+optcoins(n/3)+optcoins(n/4);
10+
return dp[n];
11+
}
12+
int main(){
13+
int x;
14+
while(scanf("%d",&x)!=EOF)
15+
printf("%lld\n",optcoins(x));
16+
return 0;
17+
}

0 commit comments

Comments
 (0)