#include "stdio.h" long reverse(long i) { long m, j=0; m = i; while(m){ j = j*10 + m%10; m = m/10; } return j; } long isCircle(long n) { long m; m = reverse(n); if(m != n) return isCircle(m)+1; else return 1; } void palindrome(long low,long high) { long i; for(i=low;i<=high;i++) { if(isCircle(i) == 3) printf("%ld ",i); } } int main() { printf("The palindrome numbers between 11~999 are\n"); palindrome(11,999); return 0; }优化代码
时间: 2023-12-10 12:02:22 浏览: 50
#include <stdio.h>
4星 · 用户满意度95%
可以考虑在判断回文数的时候,直接计算各个数位上的数字之和,判断这个数字之和是否是回文数,这样就不需要多次调用 `reverse` 函数了。
代码如下:
```c
#include "stdio.h"
long isPalindrome(long n) {
long sum = 0, m = n;
while (m) {
sum = sum * 10 + m % 10;
m /= 10;
}
return sum == n;
}
long isCircle(long n) {
long sum = 0, m = n;
while (m) {
sum += m % 10;
m /= 10;
}
return isPalindrome(sum);
}
void palindrome(long low, long high) {
long i;
for (i = low; i <= high; i++) {
if (isCircle(i)) {
printf("%ld ", i);
}
}
}
int main() {
printf("The palindrome numbers between 11~999 are\n");
palindrome(11, 999);
return 0;
}
```
阅读全文