【欧拉计划4】Largest palindrome product
原创:【欧拉计划4】Largest palindrome product
摘要:找出两个3位数乘积得到的最大回文数
作者:MilkCu
题目描述Problem 4 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.
我的解答第一次做的答案是580085,提交后提示错误,原来程序逻辑错误,得出的并不是最大的。增加max判断后,程序就对了。
# include <stdio.h>int isPal(int n);int reverse(int n);int main(void){int p;int max = 0;int i, j, step;for(i = 999; i >= 100; i--) {if(i % 11 == 0) {j = 999;step = 1;} else {j = 990;step = 11;}for(; j >= i; j--) {p = i * j;if(isPal(p)) {if(p > max) {max = p;} else {break;}}}}printf("%d\n", max);}int isPal(int n){if(n == reverse(n)) {return 1;} else {return 0;}}int reverse(int n){int r = 0;do {r = r * 10 + n % 10;} while(n /= 10);return r;}最后答案906609
(全文完)