我重新编写了它,试图变得更简单、更直接。
我想到了这个:
IDEOne Link:
#include <float.h>
#include <math.h>
#include <stdlib.h>
int onceInATram(int n) {
int y = 0;
int x = 0;
int t1 = n;
int t2 = y;
int reminder1 = 0;
int reminder2 = 0;
int sum1 = 0;
int sum2 = 0;
do {
n = n + 1; // to add 1 to the number
y = n % 1000; // This is the first 3 numbers
x = n / 1000; // This is the last 3 numbers
printf("%d is now split into %d and %d\n", n, x, y);
t1 = x;
t2 = y;
sum1 = 0;
sum2 = 0;
for(int l=0; l<3; ++l) {
reminder1 = t1 % 10;
sum1 = sum1 + reminder1;
t1 = t1 / 10;
reminder2 = t2 % 10;
sum2 = sum2 + reminder2;
t2 = t2 / 10;
}
} while (sum1 != sum2);
return 1000*x+y;
}
int main() {
int x;
scanf("%d", &x);
int result = onceInATram(x);
printf("The Final Answer is %d\n", result);
return 0;
}
123456
The Final Answer is 123501
因为
1 + 2 + 3 == 6 == 5 + 0 + 1