41 lines
813 B
C
41 lines
813 B
C
#include <stdio.h>
|
|
#include <stdbool.h>
|
|
|
|
int history[1000] = {0};
|
|
int nextIndex = 0;
|
|
|
|
int inHistory(int num) {
|
|
int i = 0;
|
|
for (i = 0; i < nextIndex; i++) {
|
|
if (history[i] == num) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
int main() {
|
|
int input = 0;
|
|
int sum = 0;
|
|
int singleDigit = 0;
|
|
scanf("%d", &input);
|
|
do {
|
|
sum = 0;
|
|
while (input > 0) {
|
|
singleDigit = input % 10;
|
|
sum += singleDigit * singleDigit;
|
|
input /= 10;
|
|
}
|
|
input = sum;
|
|
if (inHistory(sum)) {
|
|
history[nextIndex] = 0;
|
|
}
|
|
else {
|
|
history[nextIndex] = sum;
|
|
}
|
|
nextIndex++;
|
|
}
|
|
while (history[nextIndex-1] > 1);
|
|
printf("%d\n", history[nextIndex-1]);
|
|
return 0;
|
|
} |