找出回文数。求高手。
找出回文数回文数是指该整数中的各位数字顺读和倒读都一样,如2,22,121,232等,要求定义一个函数,用于在一个指定的范围中,查找满足条件的整数x,统计满足条件的整数的个数,并输出它们。
#include<stdio.h> #include<stdlib.h> #include <string.h> #include <math.h> bool change(int a) { char str[20] = {0}; char temp[20] = {0}; sprintf(str,"%d",a); int i = strlen(str)-1; int n = 0; while(i>=0) { temp[n++] = str[i]; i--; } if(0 ==strcmp(str,temp)) return true; else return false; } main() { int m,n; scanf("%d %d",&m,&n); int i,j = 0,k; for(i = m;i<=n;i++) { if(change(i)) { printf("%3d ",i); j++; } } printf("\n一共有%d\n个",j); system("pause"); }
#include<stdio.h> #include <string.h> int change(int a) { char str[20] = {0}; char temp[20] = {0}; sprintf(str,"%d",a); int i = strlen(str)-1; int n = 0; while(i>=0) { temp[n++] = str[i]; i--; } if(0 ==strcmp(str,temp)) return 1; else return 0; } void main() { int m,n; scanf("%d %d",&m,&n); int i,j = 0,k; for(i = m;i<=n;i++) { if(change(i)) { printf("%3d ",i); j++; } } printf("\n一共有%d\n个",j); }试试这个
#include <stdio.h> // 如果是回文数则返回1否则返回0 int is_palindrome(int n) { int i, j, k = 0, x = 1, y = n, z = 0; while(y /= 10) { x *= 10; } for(i = x, j = 1; i > j; i /= 10, j *= 10) { z++; if(n / i % 10 == n / j % 10) { k++; } } if(k == z) return 1; return 0; } int main(void) { printf("0 to 1000:\n"); int i = 0; while(i < 1000) { if(is_palindrome(i)) { printf("%d\n", i); } i++; } return 0; } /* Output: 1 to 10000: 0 1 2 3 4 5 6 7 8 9 11 22 33 44 55 66 77 88 99 101 111 121 131 141 151 161 171 181 191 202 212 222 232 242 252 262 272 282 292 303 313 323 333 343 353 363 373 383 393 404 414 424 434 444 454 464 474 484 494 505 515 525 535 545 555 565 575 585 595 606 616 626 636 646 656 666 676 686 696 707 717 727 737 747 757 767 777 787 797 808 818 828 838 848 858 868 878 888 898 909 919 929 939 949 959 969 979 989 999 Process returned 0 (0x0) execution time : 0.078 s Press any key to continue. */