#include<iostream.h>
#include<conio.h>
long long coins(long long n)
{
if(n>10)
return coins(n/2)+coins(n/3)+coins(n/4);
else
return n;
}
void main()
{
clrscr();
long long n;
while(cin>>n && n>0)
{
cout<<coins(n)<<"\n";
}
}
#include<iostream.h>
#include<conio.h>
long long coins(long long n)
{
if(n>10)
return coins(n/2)+coins(n/3)+coins(n/4);
else
return n;
}
void main()
{
clrscr();
long long n;
while(cin>>n && n>0)
{
cout<<coins(n)<<"\n";
}
}
Please post the question link here as well.
conio.h
header doesn’t exist in gcc compiler. So you should not use this header and any of its functions(clrscr()
has been used here) in your programs while submitting on online judges.
You working code link.
Thanks for the reply. But when I copied this code and posted it, it showed Wrong Answer!!
Brother, refer : http://discuss.codechef.com/questions/54332/bytelandian-gold-coins-using-c
The conio.h
– clrscr()
– getch()
path is not followed in Linux. By this, I mean, you need not use conio.h
and it’s functions on a Linux platform.
clrscr()
: Since you are planning on using ncurses
, there is a clear()
function included in it, which clears the screen. Just replace clrscr()
with clear()
.
getch()
: Linux provides a wide array of such functions like gets
, fgets
, scanf
and so on.
ncurses
: In Linux, ncurses
does not depend on conio
at all. So this is again unnecessary.
clrscr()
and getch()
are both part of conio.h
. It is primarily used in MS-DOS based compilers. In Linux, they are unnecessary.
Extra:-
If I start with the coin with 24 written, I split it into 12, 8 and 6 (at this stage, total = 26) but I can split the coin 12 into 6, 4 and 3. (this gives a total of 27 = (6+4+3 + 8 + 6)) so I can get total value of 27. This example should help you remove WA.
This code works fine for given test case(24).
“working code” I meant it lets you out of compilation errors that you were previously getting. I did not check if you are following correct way to solve the problem.
Here I find a test case for you where your code goes wrong… For n = 11, you can exchange them for 5+3+2 = 10 coins. But finally you are left with lower ones than you initially had. So answer is 11.
Now try for 22 if you understand the case where you are going wrong.