The given problem is simple but even though couldn’t solve it.Can someone help me with following problem…
‘4’ and ‘7’ as Magic numbers. The numbers containing only magic numbers are also magical. Given a magic number N,what could be the next magical number greater than the given number.
problem link is:http:www.codechef.com/ALKH2012/problems/NMAGIC/
You have to find the next magical number greater then the given magical number. Think about it just like bigint additions. First reverse the string. Then Check the digits. If it is 4, then directly change it to 7 and the reverse of the new string will be your answer. But if the digit is 7 you will have to change it to 4 but now you will have to keep an extra flag to also change the next digit. This happens because of a carry. After all the digits have been checked, you will have to check if the flag is true. If so then add another digit 4 to the end of the string. The reverse of this string will be your answer. Hope this helps.
No, I didn’t use bigint. I used C++. I just told you to think about the idea of considering the number as a string or bigint and how you would normally use addition operations on it. You just need to apply that logic carefully.
char ch[110];
short int t,l,i;
int main()
{
cin>>t;
getchar();
while(t–)
{
gets(ch);
l=strlen(ch);
for(i=l-1;i>=0;i–)
if(ch[i]==‘4’)
break;
if(i==-1)
{
cout<<‘4’;
for(i=0;i<l;i++)
cout<<‘4’;
}
else
{ch[i]=‘7’;
for(i=i+1;i<l;i++)
{
ch[i]=‘4’;
}
for(i=0;i<l;i++)
cout<<ch[i];}
cout<<endl;
}
}
//wats wrng in it satisfies evry thing still wrng ans
//pls give a test case which gives a wrng ans pls
I submitted the below program…but i am getting wrong answer so pls…show me the test cases where my program doesn’t work…
#include<stdio.h>
int main()
{
long long int n;
int c=0,i,t;
scanf("%d",&t);
while(t--)
{
scanf("%lld",&n);
c=0;
while(n%10!=4 && n>0)
{
n=n/10;
c++;
}
if(n!=0)
printf("%lld",n+3);
if(n==0)
c++;
for(i=0;i<c;i++)
printf("4");
}
return 0;
}
@bitfield You need to use character array to store that range of data(10^100)…no data type in C/C++ can handle it directly…so i guess your program is showing wrong answer since you have used long long int…