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