欧拉计划004--最大回文乘积

欧拉计划004--最大回文乘积

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.

最大回文乘积

回文数就是从前往后读和从后往前读都一样的数。由两个2位数相乘得到的最大的回文数是 \(9009=91×99\)

求由两个\(3\)位数相乘得到的最大的回文数。

这道题目的关键就是怎么判断是否为回文数。在这里,我们只要将一个数逆转,如果逆转后和原来相等,那么这个数就是一个回文数。接下来,我们开始遍历枚举,将所有的数求解出来,取最大的就是答案。求解出的答案为906609。

//判断是否为回文数
#include<iostream>
using namespace std;

int reverse(int);

int main(){
    int max = 0;
    for(int i=100;i<1000;i++){
        for(int j=100;j<1000;j++){
            int mul = i*j;
            int res = reverse(mul);
            if (res == mul&&mul>max){
                max = mul;
            }
        }
    }
    cout<<"最大的回文数为"<<max<<endl;
    return 0;
}

int reverse(int num){
    int a=0;
    while(num!=0){
        a = a*10+num%10;
        num/=10;
    }
    return a;
}
posted @ 2021-09-20 16:31  ChrisNg  阅读(106)  评论(0)    收藏  举报