Chinaunix首页 | 论坛 | 博客
  • 博客访问: 238000
  • 博文数量: 47
  • 博客积分: 1229
  • 博客等级: 中尉
  • 技术积分: 568
  • 用 户 组: 普通用户
  • 注册时间: 2010-09-20 10:06
文章分类

全部博文(47)

文章存档

2014年(1)

2013年(7)

2012年(1)

2011年(38)

分类: C/C++

2011-03-04 00:04:40

A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.

Find the largest palindrome made from the product of two 3-digit numbers.
--------------------

  1. #include <stdio.h>
  2. #include <string.h>

  3. void itoa(char *buf, int n)
  4. {
  5.     int i = 0;

  6.     for (i=0; n>0; i++, n /= 10) {
  7.         buf[i] = (n % 10) + '0';
  8.     }

  9.     buf[++i] = '\0';
  10. }

  11. int is_palindromic(int num)
  12. {
  13.     char buf[sizeof(num)*8];
  14.     int len;
  15.     int i;

  16.     itoa(buf, num);
  17.     len = strlen(buf);
  18.     for (i=0; i<(len+1)/2; i++) {
  19.         if(buf[i] != buf[len-i-1])
  20.             return 0;
  21.     }

  22.     return 1;
  23. }

  24. int main(int argc, const char *argv[])
  25. {
  26.     int i, j;
  27.     int result;
  28.     int acc, mi, mj;

  29.     for (i = 999; i > 100; i--) {
  30.         for (j = i; j > 100; j--) {
  31.             acc = i * j;
  32.             if (is_palindromic(acc) ) {
  33.                 if (acc > result) {
  34.                     result = acc;    
  35.                     mi = i, mj = j;

  36.                     printf("result: %d, i %d, j %d\n", result, i, j);
  37.                 }
  38.             }
  39.         }
  40.     }

  41.     printf(" %d = %d x %d\n", result, mi, mj);

  42.     return 0;
  43. }

  44. 根据乘法交换律筛选了一些。再依据或然率可进一步优化。

阅读(1170) | 评论(0) | 转发(0) |
0

上一篇:euler10

下一篇:euler13

给主人留下些什么吧!~~