首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 其他相关 >

【欧拉计划四】Largest palindrome product

2013-09-06 
【欧拉计划4】Largest palindrome product原创:【欧拉计划4】Largest palindrome product摘要:找出两个3位数乘

【欧拉计划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

(全文完)

热点排行