欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 房产 > 家装 > 欧拉计划 Project Euler 22 题解

欧拉计划 Project Euler 22 题解

2025/8/26 8:03:44 来源:https://blog.csdn.net/m0_53387935/article/details/144469786  浏览:    关键词:欧拉计划 Project Euler 22 题解

欧拉计划 Problem 21 题解

  • 题干
  • 思路

最近比较忙 只能一天一题了

题干

在这里插入图片描述

思路

可以读文件直接暴力枚举 也可以排序 用python好做一点
这里给出c++和python代码
c++

#include <bits/stdc++.h>using namespace std;using ll = long long;vector<int> val(26, 0);
// 871198282
void solve() {// 注意到文件中全是大写 所以无需进行转换iota(val.begin(), val.end(), 1);string s = "COLIN";ll ans = 0;string names[10000];int n = 0;while (cin >> names[n]) {n++;}sort(names, names + n); // 可以排序统计每个字符for (int i = 0; i < n; ++i) {int t = 0;for (int j = 0; j < names[i].size(); ++j) {t += names[i][j] - 'A' + 1;}ans += t * (i + 1);}cout << ans << "\n";}int main() {ios::sync_with_stdio(false);cin.tie(nullptr);int tt = 1; // cin >> tt;while (tt--) {solve();}return 0;
}

python

f = open("p022_names.txt", encoding="utf-8")
a = list(map(str, f.read().split(',')))
a.sort()
idx = 1
res = 0
for i in a:s = 0for j in i:if j == '"' : continuex = ord(j) - ord('A') + 1s += xres += s * idxidx += 1
print(res)

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词