全部博文(68)
分类: C/C++
2012-01-18 20:01:07
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.
答案: 906609
#include
int reverse(int n)
{
int reversed = 0;
while(n>0){
reversed = 10*reversed + n%10;
n /= 10;
}
return reversed;
}
int isPalindrom(int n)
{
return n==reverse(n);
}
int main(void)
{
int largest = 0;
int a = 999;
int b = 999;
int db = 1;
while(a>=100){
if((a%11) == 0)
b = 999, db = 1;
else
b = 990, db = 11;
while(b>=a){
if(a*b < largest)
break;
if(isPalindrom(a*b))
largest = a*b;
b -= db;
}
a--;
}
printf("%d\n", largest);
return 0;
}