Lesson 9。
This commit is contained in:
41
09/Optional01.c
Normal file
41
09/Optional01.c
Normal file
@@ -0,0 +1,41 @@
|
||||
#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;
|
||||
}
|
||||
Reference in New Issue
Block a user