我试图解决问题4"最大的回文产品"在ProjectEuler中,当我运行下面的程序时,结果为0,我尝试gdb这个程序,它太奇怪了,
(gdb) start
Temporary breakpoint 1 at 0x4004df: file largest_palindrome_product.c, line 5.
Starting program: /home/ljhui/Git/MyGit/ProjectEuler/lpp
Temporary breakpoint 1, main () at largest_palindrome_product.c:5
5 int sum=0;
(gdb) n
6 int max_p=0;
(gdb)
8 for(i=100;i<1000;i++)
(gdb)
10 for(j=i;j<1000;j++)
(gdb) p i
$1 = 100
(gdb) p j
$2 = 0
(gdb) n
12 integer = i*j;
(gdb) p integer
$3 = 4195328
正如你所看到的,价值不是预期的,哪里是错的,我是一个编程的菜鸟
#include<stdio.h>
int main()
{
int integer,m;
int sum=0;
int max_p=0;
int i,j;
for(i=100;i<1000;i++)
{
for(j=i;j<1000;j++)
{
integer = i*j;
m = integer;
while(m)
{
sum = sum*10 + m%10;
m/=10;
}
if(sum==integer)
{
if (integer > max_p)
{
max_p = integer;
}
}
}
}
printf("%d\n",max_p);
return 0;
}
答案 0 :(得分:1)
#include<stdio.h>
int main()
{
int integer,m;
int sum=0;
int max_p=0;
int i,j;
for(i=100;i<1000;i++)
{
for(j=i;j<1000;j++)
{
sum = 0;
integer = i*j;
m = integer;
while(m)
{
sum = sum*10 + m%10;
m/=10;
}
if(sum==integer)
{
if (integer > max_p)
{
max_p = integer;
}
}
}
}
printf("%d\n",max_p);
return 0;
}