C中拆分数字的总和
sum of splitted digits in C
我想将给定数字(6 位数字)在将其递增 1 后拆分为 2 个数字(每个数字 3 位数字)然后将前 3 位数字和后 3 位数字相加并检查总和是否匹配,执行该过程再次,最后 return 首位和末位 3 位数字之和相等的数字。
我的代码拆分了数字,但最后 3 位数字以某种方式打印相反(这不是问题,因为我只想要它们的总和)
但是当我尝试对每 3 位数字求和时,问题就来了。
int onceInATram(int x) {
// Complete this function
int n = 0;
int y = 0;
int len = 0;
int digit = 0;
int t1 = n;
int t2 = y;
int reminder1 = 0;
int reminder2 = 0;
int sum1 = 0;
int sum2 = 0;
len = (int) floor(log10(abs(x))) + 1;
do {
n = x + 1; // to add 1 to the number
while ((floor(log10(abs(n)) + 1) > len / 2)) { // split it by half
digit = n % 10;
n = n / 10;
y = (y * 10) + digit;
}
int l = 3;
while (l--) {
reminder1 = t1 % 10;
sum1 = sum1 + reminder1;
t1 = t1 / 10;
reminder2 = t2 % 10;
sum2 = sum2 + reminder2;
t2 = t2 / 10;
}
} while (sum1 != sum2);
//return(printf("%d\n%d\n", n, y)); // for debugging
return printf("%d%d\n", n, y); // '' ''
//return printf("%d\n", sum1); // '' ''
}
int main() {
int x;
scanf("%i", &x);
int result_size;
char* result = (char *) onceInATram(x);
printf("%s\n", result);
return 0;
}
并且我使用了函数,但似乎没有任何作用!
我的输入:
555555
我的输出:555655 > 与 555556 相同(递增 1 但反转最后 3 位数字)。
预期输出:555564(前 3 位数字的总和 == 后 3 位数字)。
我重写了它以尽量简单明了。
我想到了这个:
#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
我想将给定数字(6 位数字)在将其递增 1 后拆分为 2 个数字(每个数字 3 位数字)然后将前 3 位数字和后 3 位数字相加并检查总和是否匹配,执行该过程再次,最后 return 首位和末位 3 位数字之和相等的数字。
我的代码拆分了数字,但最后 3 位数字以某种方式打印相反(这不是问题,因为我只想要它们的总和)
但是当我尝试对每 3 位数字求和时,问题就来了。
int onceInATram(int x) {
// Complete this function
int n = 0;
int y = 0;
int len = 0;
int digit = 0;
int t1 = n;
int t2 = y;
int reminder1 = 0;
int reminder2 = 0;
int sum1 = 0;
int sum2 = 0;
len = (int) floor(log10(abs(x))) + 1;
do {
n = x + 1; // to add 1 to the number
while ((floor(log10(abs(n)) + 1) > len / 2)) { // split it by half
digit = n % 10;
n = n / 10;
y = (y * 10) + digit;
}
int l = 3;
while (l--) {
reminder1 = t1 % 10;
sum1 = sum1 + reminder1;
t1 = t1 / 10;
reminder2 = t2 % 10;
sum2 = sum2 + reminder2;
t2 = t2 / 10;
}
} while (sum1 != sum2);
//return(printf("%d\n%d\n", n, y)); // for debugging
return printf("%d%d\n", n, y); // '' ''
//return printf("%d\n", sum1); // '' ''
}
int main() {
int x;
scanf("%i", &x);
int result_size;
char* result = (char *) onceInATram(x);
printf("%s\n", result);
return 0;
}
并且我使用了函数,但似乎没有任何作用!
我的输入: 555555
我的输出:555655 > 与 555556 相同(递增 1 但反转最后 3 位数字)。
预期输出:555564(前 3 位数字的总和 == 后 3 位数字)。
我重写了它以尽量简单明了。
我想到了这个:
#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