假设我们在C语言中有这样一个程序,它判断一个数字是否回文,根本不使用任何数组(所以不要使用任何使用数组的答案)
#include <stdio.h>
#include <math.h>
int NumOfDigits(long long x) {
int sum = 1;
while (x / 10 != 0) {
sum ++;
x = x / 10;
}
return sum;
}
int isPal(long long x) {
int f, c, front, back, sum;
sum = NumOfDigits(x);
c = round(pow(10,(sum-2)));
front = x / round(pow(10,(sum - 1)));
back = x % 10;
f = 1;
while (x != 0 && f == 1 && c != 0) {
if (front == back) {
x = (x / 10) % c;
c /= 100;
sum -=2;
front = x / round(pow(10,(sum-1)));
back = x % 10;
} else {
f = 0;
}
}
if (f) {
return 1;
} else {
return 0;
}
}
int main() {
int f;
long long x;
scanf("%lld", &x);
f = isPal(x);
if (f) {
printf("yes");
} else {
printf("no");
}
printf("\n");
}
基本上,这个算法每次都会判断第一个和最后一个数字,然后将数字减go 2,所以如果我们有345543,首先是345543,然后是4554,55等等.这个程序的要点是,例如,给定数字900075181570009,它说它不是回文,但这是因为计算机删除了数字左侧的0.所以当它达到0007518157000的时候,基本上就是7518157000,这不是一个回文数字.那么,我们如何在不使用数组的情况下修改算法,以达到预期的结果呢?