欧拉计划第4题:Largest palindrome product(枚举 回文数)

该篇文章介绍了如何使用编程解决ProjectEuler问题4,即寻找由两个3位数相乘得到的最大回文数。通过枚举100到999之间的数,检查其乘积是否为回文,找到并输出结果9009=91×99。

Problem 4:Largest palindrome product

标签:枚举、回文数

原文:A palindromic number reads the same both ways. The largest palindrome made from the product of two 222-digit numbers is 9009=91×999009 = 91 \times 999009=91×99.

Find the largest palindrome made from the product of two 333-digit numbers.

翻译:回文数是指从前往后读和从后往前读都一样的数。由两个 222 位数相乘得到的回文数中,最大的是 9009=91×999009 = 91 \times 999009=91×99

求最大的由两个 333 位数相乘得到的回文数。

题解:直接枚举两个在 100100100 ~ 999999999 之间的数,对它们的乘积做一个回文数判定,数据范围比较小,可以直接数字翻转一下判断。

代码

#include <bits/stdc++.h>
using namespace std;

bool check(int a) {
    int b = a, c = 0;
    while (b > 0) {
        c = 10 * c + b % 10;
        b /= 10;
    }
    return c == a;
}

int main() {
    int mx = 0;
    for (int i = 100; i <= 999; i++)
    for (int j = 100; j <= 999; j++) {
        if (check(i * j)) mx = max(mx, i * j);
    }
    cout << mx << endl;
    return 0;
}

“Project Euler exists to encourage, challenge, and develop the skills and enjoyment of anyone with an interest in the fascinating world of mathematics.”

“欧拉计划的存在,是为了每个对数学感兴趣的人,鼓励他们,挑战他们,并最终培养他们的能力与乐趣。”

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值