首页 > 代码库 > Largest palindrome product

Largest palindrome product

problem 4:Largest palindrome product

题意:求由三位数和三位数之积构成的最大回文数字

代码如下:

 1 #ifndef PRO4_H_INCLUDED
 2 #define PRO4_H_INCLUDED
 3 
 4 namespace pro4{
 5     bool ispalindromic(int n){
 6         int a[6],k=0;
 7         while(n){
 8             a[k++]=n%10;
 9             n/=10;
10         }
11         for(int i=0;i<k/2;++i)
12             if(a[i]!=a[k-1-i])return 0;
13         return 1;
14     }
15     int solve(){
16         int maxn=0;
17         for(int i=100;i<=999;++i){
18             for(int j=100;j<=999;++j){
19                 int t=i*j;
20                 if(ispalindromic(t)&&t>maxn)maxn=t;
21             }
22         }
23         return maxn;
24     }
25 }
26 
27 #endif // PRO4_H_INCLUDED

 

Largest palindrome product