Advertisement
stream13

find max palindrome

Apr 13th, 2013
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.94 KB | None | 0 0
  1. #include "stdafx.h"
  2.  
  3. bool is_pal(char * str);
  4. void reverse(char *x, int begin, int end);
  5. void reverse(char *x);
  6.  
  7. int _tmain(int argc, _TCHAR* argv[]){
  8.     char numc[10];
  9.     sprintf(numc,"%d",123456);
  10.     int a=999,b=999,numi = 0, found = 0;
  11.     while (b > 99){
  12.         for (int i = a; i >= b; --i ){
  13.             numi = b * i;
  14.             sprintf(numc, "%d", numi);
  15.             if (is_pal(numc) == 0){
  16.                 if(numi > found){
  17.                     found = numi;
  18.                 }
  19.                 //printf("found!\n");
  20.             }
  21.         }
  22.         --b;
  23.     }
  24.     printf("palindrome is '%d'\n", found);
  25.     return 0;
  26. }
  27.  
  28. bool is_pal(char * str){
  29.     size_t len = strlen(str);
  30.     char * rev;
  31.     rev = new char[len+1];
  32.     strcpy(rev, str);
  33.     reverse(rev);
  34.     bool it_is = strcmp(str,rev);
  35.     delete [] rev;
  36.     return it_is;
  37. }
  38.  
  39. void reverse(char *x){
  40.     reverse(x, 0, strlen(x)-1);
  41. }
  42.  
  43. void reverse(char *x, int begin, int end){
  44.     char c;
  45.     if (begin >= end)
  46.         return;
  47.     c = *(x+begin);
  48.     *(x+begin) = *(x+end);
  49.     *(x+end) = c;
  50.  
  51.     reverse(x, ++begin, --end);
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement