最近我写了这个quick'n'dirty代码,它产生如下的回文:
int main() {
generateEvenPalindromes(1);
}
unsigned long generateEvenPalindromes(int lowerBound) {
unsigned long palindrome = 0;
int upperBound = lowerBound * 10;
for (int i = lowerBound; i < upperBound; i++) {
palindrome = appendMirroredValueToNum(i, -1); // -1 means 'no middle digit'
printf("%lu\n", palindrome);
}
return generateOddPalindromes(lowerBound);
}
unsigned long generateOddPalindromes(int lowerBound) {
unsigned long palindrome = 0;
int upperBound = lowerBound * 10;
for (int i = lowerBound; i < upperBound; i++) {
for (int j = 0; j < 10; j++) {
palindrome = appendMirroredValueToNum(i, j);
printf("%lu\n", palindrome);
}
}
if (upperBound > 10000) {
return 0;
}
return generateEvenPalindromes(upperBound);
}
unsigned long appendMirroredValueToNum(int num, int middleDigit) {
//...
return newNumber;
}
- 它产生一系列 aa 和 aba 回文序列,其中a [1; 99999],b [0; 9],即
11
22
...
99
101
111
121
...
217712
218812
219912
...
99999899999
99999999999
- 我正在考虑使用函数 generateEvenPalindromes()和 generateOddPalindromes()并将它们合并为一个。如果您认为这是个好主意,请您推荐一个好的方法来实现这个目标吗?