我正在尝试从项目euler解决问题4(问:找到由两个3位数字的乘积制成的最大回文)。我得到580085作为答案,但这不正确。我做错了什么?不寻找不同的实现只是想知道我的实现有什么不对。
#include <stdio.h>
int reverse_num(int num)
{
int ret = 0, value = 1, temp = 0;
temp = num;
while(temp>0)
{
temp = temp/10;
value = value * 10;
}
value = value/10;
while(num>0)
{
temp = num % 10;
temp = temp * value;
ret = ret + temp;
num = num/10;
value = value/10;
}
return ret;
}
int check_palindrome(int num)
{
if(num==reverse_num(num))
return 1;
else
return 0;
}
int main(void)
{
int num = 0, i = 0, j = 0, temp = 0;
for(i=100; i<1000; i++)
{
for(j=100; j<1000; j++)
{
temp = i * j;
if(check_palindrome(temp))
{
printf("found %d palindrome, its product of %d and %d\n", temp, i, j);
}
}
}
return 0;
}
答案 0 :(得分:1)
即使您可以更有效地执行此操作,您的反向算法仍然有效。
select distinct Funded_Amount + Direct_Amount as Fuel,
from ....
我认为您将获得多条印刷线。您可能还会发现正确答案不一定是打印的最后答案,特别是当您计算出每个产品两次时。
修改
在我写完这个答案之后,问题得到了修正。问题中的代码现在有效。
答案 1 :(得分:0)
#include <stdio.h>
int reverse_num(int num)
{
int ret = 0, value = 1, temp = 0;
temp = num;
while(temp>0)
{
temp = temp/10;
value = value * 10;
}
value = value/10;
while(num>0)
{
temp = num % 10;
temp = temp * value;
ret = ret + temp;
num = num/10;
value = value/10;
}
return ret;
}
int check_palindrome(int num)
{
if(num==reverse_num(num))
return 1;
else
return 0;
}
int main(void)
{
int num = 0, i = 0, j = 0, temp = 0, largest = 0;
for(i=100; i<1000; i++)
{
for(j=100; j<1000; j++)
{
temp = i * j;
if(check_palindrome(temp))
{
if(temp>largest)
{
largest = temp;
printf("found largest %d palindrome, its product of %d and %d\n", largest, i, j);
}
//printf("found %d palindrome, its product of %d and %d\n", temp, i, j);
}
}
}
return 0;
}