btw....i have already spectacles....seems the power got increased
Moderator: Board moderators
2
7
7
AC. Thanks Helloneo
1
4Case #1: 4 is an Unhappy number.helloneo wrote:Try this input..
- Code: Select all
1
4
My output..
- Code: Select all
Case #1: 4 is an Unhappy number.
PS. Remove you code after AC..
int arr[2000];
............
..........
if(arr[n]==1)
...............
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
using namespace std;
#define maxc 100000
#define maxl 100000
long Strrev(char *Str)
{
char Temp[maxc]={'\0'};
long l=strlen(Str),i;
for(i=0;i<l;i++)
{
Temp[i]=Str[l-i-1];
}
Temp[i]='\0';
strcpy(Str,Temp);
return 0;
}
int main()
{
char in[maxc], out[maxc];
long store[maxl];
long len, ho1=0, i=0, j=0, hold, get, sum, let=0, hv, kk=0, tt, mm, flag=0, test1, test=1, ii;
long o, p;
scanf("%ld", &test1);
for(ii=0; ii<test1; ii++)
{
memset(store, '0', sizeof(store));
memset(out, '0', sizeof(out));
memset(in, '0', sizeof(in));
flag=0;
kk=0;
scanf("%s", in);
mm=atol(in);
store[kk]=mm;
kk++;
ho1=atol(in);
len=strlen(in);
while(1)
{
sum=0;let=0;
for(i=0; i<len; i++)
{
let=in[i]-'0';
sum+=pow(let, 2);
}
if(sum==1)
{
get=1;
break;
}
else if(sum==ho1)
{
get=ho1;
break;
}
else
{
j=0;
memset(out, '0', sizeof(out));
while(1)
{
out[j]=(sum%10)+'0';
hold=sum/10;
sum=hold;
if(hold==0)
{
j++;
break;
}
j++;
}
out[j]='\0';
Strrev(out);
hv=atol(out);
store[kk]=hv;
kk++;
for(tt=0; tt<kk-1; tt++)
{
if(store[tt]==hv)
{
flag=1;
break;
}
}
if(flag==1)
{
get=ho1;
break;
}
strcpy(in, out);
len=strlen(in);
}
}
if(get==1)
printf("Case #%ld: %ld is a Happy number.\n", test++, ho1);
else if(get==ho1)
printf("Case #%ld: %ld is an Unhappy number.\n", test++, ho1);
}
return 0;
}#include<stdio.h>
#define max 10000
long unhappy(long n);
long search(long t[max],long s,long c);
long m,c,t[max],j;
main()
{
long x;
scanf("%ld",&x);
for(j=1;j<=x;j++)
{
scanf("%ld",&m);
c=1;
unhappy(m);
}
return 0;
}
long unhappy(long n)
{
long p,q,sum;
if(n>9)
{
sum=0;
while(n!=0)
{
p=n%10;
q=p*p;
sum=sum+q;
n=n/10;
}
}
else
sum=n*n;
t[c]=sum;
c=c+1;
search(t,t[c-1],c-1);
return 0;
}
long search(long t[max],long s,long u)
{
long i;
if(s==1)
{
printf("Case #%ld: %ld is a Happy Number.\n",j,m);
return 0;
}
else
{
for(i=1;i<u;i++)
if(s==t[i])
{
printf("Case #%ld: %ld is an Unhappy number.\n",j,m);
return 0;
}
return unhappy(s);
}
}
Users browsing this forum: No registered users and 1 guest