Project Euler——Problem 4 - Daybreakcx's Blog - Keep Programming! With Algorithm! With Fun!
Project Euler——Problem 4
daybreakcx
posted @ 2009年7月14日 01:36
in Prject Euler
, 994 阅读
原题与答案:
Problem 4
16 November 2001
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.
Answer:906609
分析与解答:
这个题目也是很简单的,题目求两个三位数,使得他们的乘积是回文数,要求返回最大的满足条件的回文数,这里只需要枚举每对三位数,并且对他们的乘积进行判定输出最大值即可,编码没有什么难度,以下是实现代码:
-
#include<stdio.h>
-
int num[10],ans,i,j,s;
-
bool ispalindrome(int s)
-
{
-
int i,j;
-
num[0]=0;
-
while (s)
-
{
-
num[++num[0]]=s%10;
-
s/=10;
-
}
-
for (i=1,j=num[0];i<j;i++,j--)
-
if (num[i]!=num[j]) return false;
-
return true;
-
}
-
int main()
-
{
-
ans=0;
-
for (i=100;i<=999;i++)
-
for (j=i;j<=999;j++)
-
{
-
s=i*j;
-
if (s>ans&&ispalindrome(s)) ans=s;
-
}
-
return 0;
-
}