Project Euler 4

 

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.

今天做了PE的第四道题,遇到了一些麻烦,不过比第三题要好些。

基于第三题的惨痛经历,拿着这道题,总觉得会有一些很tricky的方法,结果想了半天没有进展。结果看了看别人的解法,原来还是遍历……

1.找出回文数,因为题上要求是最大的三位数乘积回文数,所以就从大往小遍历,三位数最大乘积998001,所以就从998001到100000.
判断一个数是否是回文数的程序:

  int Palindromic(int n)
  {
    int palindromic = 0;
    int first,second,third,fourth,fifth,sixth;
    first = n/100000;
    second = n%100000/10000;
    third = n%10000/1000;
    fourth = n%1000/100;
    fifth = n%100/10;
    sixth = n%10;
    if(first == sixth)
        if(second == fifth)
            if(third == fourth)palindromic = 1;
    return(palindromic);
  }

2.判断这个回文数能否分为两个三位数之积。让i从100到999,若能除尽且商也在100到999之间则输出此时的回文数并终止程序。

  for(n = 998001; n >= 100000; n--)
    {
        if(Palindromic(n))
            {
                for(i = 100; i < = 999; i++)                     
                    {                         
                        if(n%i == 0)                         
                            {                             
                                if((n/i >= 100)&&(n/i < = 999))
                            {
                                printf("the number is %d\n", n);
                                printf("%d\n",i);
                                exit(0);
                            }
                        }
                    }
            }
    }


 

作者:michaelxi007 发表于2013-11-17 23:14:01 原文链接
阅读:23 评论:0 查看评论

 

posted on 2013-11-17 23:14  清水老和尚  阅读(176)  评论(0编辑  收藏  举报

导航