欧拉计划 Project Euler 56 题解
- 题干
- 思路
- code
题干
思路
直接暴力枚举即可,用c++要模拟大数的乘法,否则会溢出
code
// 972
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
string mul(const string &num1, int num2) {
int carry = 0; // 进位
string ans;
for (int i = num1.size() - 1; i >= 0; --i) {
int tmp = (num1[i] - '0') * num2 + carry;
ans += (tmp % 10) + '0';
carry = tmp / 10;
}
while (carry > 0) {
ans += (carry % 10) + '0';
carry /= 10;
}
reverse(ans.begin(), ans.end());
return ans;
}
int digsum(const string &num) {
int ans = 0;
for (char c : num) {
ans += c - '0';
}
return ans;
}
void solve() {
ll ans = 0;
for (int a = 1; a < 100; ++a) {
string power = "1";
for (int b = 1; b < 100; ++b) {
power = mul(power, a);
int sum = digsum(power);
if (sum > ans) {
ans = sum;
}
}
}
cout << ans << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int tt = 1; // cin >> tt;
while (tt--) {
solve();
}
return 0;
}