Problem link-Click here
it's an easy implement based problem.just follow the statement carefully. you need to process 100 number back to the given number to get desired result. for example if n=2005 then you have to check 2005-100=1905 to 2004 to get the generator of 2005. there's a constraint and which is if n is less than 100 then you have to go to n/2 to n-1 to get the generator and if can't be found just print 0.
an accepted code is as follows:
#include<bits/stdc++.h>
using namespace std;
int process(int n)
{
int sum=n;
while(n!=0)
{
sum=sum+(n%10);
n=n/10;
}
return sum;
}
int main()
{
int t,n;
scanf("%d",&t);
while(t--)
{
bool flag=0;
int s;
scanf("%d",&n);
if(n>100)
{
for(int i=n-100;i<n;i++)
{
if(process(i)==n)
{
flag=1;
s=i;
break;
}
}
}
else
{
for(int i=n/2;i<n;i++)
{
if(process(i)==n)
{
flag=1;
s=i;
break;
}
}
}
if(flag==1)
{
printf("%d\n",s);
}
else
{
printf("%d\n",0);
}
}
return 0;
}
it's an easy implement based problem.just follow the statement carefully. you need to process 100 number back to the given number to get desired result. for example if n=2005 then you have to check 2005-100=1905 to 2004 to get the generator of 2005. there's a constraint and which is if n is less than 100 then you have to go to n/2 to n-1 to get the generator and if can't be found just print 0.
an accepted code is as follows:
#include<bits/stdc++.h>
using namespace std;
int process(int n)
{
int sum=n;
while(n!=0)
{
sum=sum+(n%10);
n=n/10;
}
return sum;
}
int main()
{
int t,n;
scanf("%d",&t);
while(t--)
{
bool flag=0;
int s;
scanf("%d",&n);
if(n>100)
{
for(int i=n-100;i<n;i++)
{
if(process(i)==n)
{
flag=1;
s=i;
break;
}
}
}
else
{
for(int i=n/2;i<n;i++)
{
if(process(i)==n)
{
flag=1;
s=i;
break;
}
}
}
if(flag==1)
{
printf("%d\n",s);
}
else
{
printf("%d\n",0);
}
}
return 0;
}
No comments:
Post a Comment