-
Notifications
You must be signed in to change notification settings - Fork 87
/
1024.c
62 lines (51 loc) · 1 KB
/
1024.c
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
#include <stdio.h>
#include <string.h>
int isPalindromic(char n[])
{
int len = strlen(n);
for (int i = 0; i < len / 2; i++)
if (n[i] != n[len - i - 1])
return 0;
return 1;
}
char* reverse(char n[])
{
char temp;
int len = strlen(n);
for (int i = 0; i < len / 2; i++) {
temp = n[i];
n[i] = n[len - i - 1];
n[len - i - 1] = temp;
}
return n;
}
/* only works when a and b are of the same length */
void addAtoB(char a[], char b[])
{
int l, s = 0, len = strlen(a);
for (int i = 0; i < len; i++) {
s += (a[i] - '0') + (b[i] - '0');
l = s / 10;
s %= 10;
b[i] = s + '0';
s = l;
}
if (s)
b[len] = s + '0';
}
int main()
{
int K, steps;
char s1[100] = {0}, s2[100] = {0}, *n = s1, *m = s2;
scanf("%s %d", n, &K);
reverse(n);
for (steps = 0; steps < K && !isPalindromic(n); steps++) {
/* change 'm' into reverse of 'n' */
strncpy(m, n, 100);
reverse(m);
/* add n and reversed n, and keep the result in n */
addAtoB(m, n);
}
printf("%s\n%d", reverse(n), steps);
return 0;
}