题目4:找出由两个三位数乘积构成的回文。
一个回文数指的是从左向右和从右向左读都一样的数字。最大的由两个两位数乘积构成的回文数是9009 = 91 * 99.
找出最大的有由个三位数乘积构成的回文数。
源码
STDMETHODIMP COuLa::Test4(int iMaxNumber)
{
// TODO: 在此加入实现代码
int outputNumber = 0;
int iMaxOutputNumber = 0;
for(int i = iMaxNumber/10; i<iMaxNumber; i++)
{
for(int j = i; j<iMaxNumber; j++)
{
outputNumber = j*i;
int iFirstNumber = outputNumber;
int iSecondNumber = iFirstNumber;
int iThirdNumber = iSecondNumber;
while(iFirstNumber > 9)
{
iThirdNumber = iSecondNumber%10;
iSecondNumber = iFirstNumber%10;
iFirstNumber = iFirstNumber/10;
}
//printf("iFirstNumber is %d.\n", iFirstNumber);
if(outputNumber%10 == iFirstNumber && (outputNumber/10%10) == iSecondNumber && (outputNumber/100%10) == iThirdNumber)
{
if(iMaxOutputNumber < outputNumber) iMaxOutputNumber = outputNumber;
}
}
}
printf("The Test 4 Number is %d.\n",iMaxOutputNumber);
return S_OK;
}
函数调用
#define TEST4_NUMBER 1000
IOula->Test4(TEST4_NUMBER);
输出
The Test 4 Number is 906609.