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

Problem 4:Largest palindrome product

标签:枚举、回文数

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

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

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

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

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

代码

#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.”

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

  • 18
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值