-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10018(rev&add).cpp
More file actions
77 lines (72 loc) · 954 Bytes
/
10018(rev&add).cpp
File metadata and controls
77 lines (72 loc) · 954 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include<stdio.h>
#include<string.h>
void add(char x[]);
int main()
{
char x[1001],y[1001];
int i,j,test,count,k;
scanf("%d",&test);
while(test>0)
{
scanf("%s",x);
i=strlen(x);
k=0;
for(j=i-1;j>=0;j--)
{
y[k]=x[j];
k++;
}
y[k]='\0';
count=0;
while(strcmp(x,y)!=0)
{
count++;
add(x);
i=strlen(x);
k=0;
for(j=i-1;j>=0;j--)
{
y[k]=x[j];
k++;
}
y[k]='\0';
}
printf("%d ",count);
printf("%s",x);
test--;
printf("\n");
}
return 0;
}
void add(char x[])
{
int s,h,a,o,n,t,carry=0,k,j;
char add_result[1001],y[1001];
s=strlen(x);
k=0;
for(j=s-1;j>=0;j--)
{
y[k]=x[j];
k++;
}
y[k]='\0';
t=0;
while(s>0)
{
h=x[s-1]-'0';
a=y[s-1]-'0';
o=h+a+carry;
n=o%10;
carry=o/10;
add_result[t]=n+'0';
t++;
s--;
}
if(carry>0)
{
add_result[t]=carry+'0';
t++;
}
add_result[t]='\0';
strcpy(x,add_result);
}