Problem4-Project Euler

Largest palindrome product

 

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.

寻找能够分解为两个三位数乘积的回文数:

先分析,回文数范围在:100^2=10000到999^2=998001之间,暴力也可行……但适当剪枝会快一些

考虑100000及以上时,回文数能被11整除,在循环中就可以抛去其它部分数,解答会快一些。

还想用用时间换空间可以先打个质数表……之类的

 #include"stdio.h"
#include"stdlib.h"
#include"string.h" int palindrome(int a,int b) /*palindrome function:to test if a product is palindrome*/
{
int sum,i,j;
char num[];
sum=a*b;
itoa(sum,num,);
for(i=,j=strlen(num)-;i<j;i++,j--)
if(num[i]!=num[j])
return();
return();
} int main() /*problem4-Largest palindrome product*/
{
int i,j,max=,m1=,m2=;
for(i=;i>=;i--)
for(j=i;j>=;j--)
{
if(i%!=&&j%!=) //剪枝,i,j之中必有一个为11倍数
continue;
if(palindrome(i,j)==&&i*j>max)
{
max=i*j;
m1=i;
m2=j;
}
}
printf("max palindrome is %d=%d*%d\n",max,m1,m2);
return();
}
上一篇:win10专业版密钥激活


下一篇:BZOJ 2303 方格染色