更偏向编程
值得一看
D 字符串匹配
#include <iostream> #include <string> using namespace std; int main() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); int T; cin >> T; cin.get(); while (T--) { string word, sentence; getline(cin, word); getline(cin, sentence); /*全部化为小写*/ for (string::iterator i1 = word.begin(); i1 != word.end(); i1++) *i1 = tolower(*i1); for (string::iterator i2 = sentence.begin(); i2 != sentence.end(); i2++) *i2 = tolower(*i2); /*关键:去除局部匹配*/ word = ' ' + word + ' '; sentence = ' ' + sentence + ' '; int i = sentence.find(word); int position = i; //第一次出现的位置 int cnt = 0; while (i != string::npos) { i = sentence.find(word, i + 1); //从i+1开始匹配 cnt++; } if (cnt == 0) cout << -1 << endl; else cout << cnt << " " << position << endl; } return 0; }
H 全排列
一个仅由0-9组成的字符串,对于它所有排列所构成的数字按升序排列后的序列,第n个数是多少呢?
#include <bits/stdc++.h> #include <sstream> using namespace std; typedef long long ll; const int N = 1e5 + 7; const ll mod = 1e9 + 7; int main() { int T; cin >> T; for (int ca = 1; ca <= T; ++ca) { string a; int n; cin >> a >> n; int len = a.length(); int cnt = 0; cout << "Case " << ca << ": "; sort(a.begin(),a.end()); do { cnt++; if (cnt == n) { int ans = 0; stringstream s; s << a; s >>ans; cout << ans << endl; break; } } while (next_permutation(a.begin(), a.end())); if (cnt < n) cout << "no solution" << endl; } return 0; }
string的一些技巧
https://blog.csdn.net/xiong452980729/article/details/61677701
G 数学题
本题可打表。
通过倍数递增的循环完成f数组的预处理,然后推过去就可以了。
#include <bits/stdc++.h> using namespace std; typedef long long ll; inline ll read() { ll s = 0, f = 1; char ch; do { ch = getchar(); if (ch == '-') f = -1; } while (ch < 48 || ch > 57); while (ch >= 48 && ch <= 57) s = (s << 1) + (s << 3) + (ch ^ 48), ch = getchar(); return s * f; } const int MOD = 1e9 + 7; const int N = 1e6 + 5; ll sum[N]; void init() { fill(sum, sum + N, 1); for (int i = 1; i < N; ++i)//此处处理为点睛之笔 for (int j = i; j < N; j += i) sum[j] = sum[j] * i % MOD; } int main() { init(); int T = read(); while (T--) { int l = read(), r = read(); ll ans = 1; for (int i = l; i <= r; ++i) ans = (ans * sum[i]) % MOD; printf("%lld\n", ans); } return 0; }
水题
A 模拟
t=int(input()) for _ in range(t): n=int(input()) ans=0 while n!=1: if n%2 : n-=1 ans+=1 n//=2 ans+=1 print(ans)
B 字符串key-value
dict={'茕': 'qiong','孑': 'jie','立': 'li','沆': 'hang','瀣': 'xie','一': 'yi','气': 'qi','踽':'ju','独':'du','行': 'xing','醍': 'ti','醐': 'hu','灌': 'guan','顶':'ding','绵':'mian','瓜': 'gua','瓞': 'die','奉': 'feng','为': 'wei','圭': 'gui','臬': 'nie'} for _ in range(int(input())): print(dict[input()])
C 坑题
给你一组数据,取出任意个数,使得他们的和相加最大且为奇数。
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) sum = 0 for i in a: if i > 0:sum += i if sum & 1==0: a=sorted(a, key=abs) for i in a: if i&1: sum-=abs(i) break print(sum)
I GPArank
#include <bits/stdc++.h> using namespace std; typedef long long ll; double eps = 1e-7; struct Node { string id; double score; }; vector<Node> p; int a[15]; int b[4]; bool cmp(Node a, Node b) { if (fabs(a.score - b.score) > eps) return a.score > b.score; return a.id < b.id; } int main() { int T; cin >> T; while (T--) { p.clear(); int n, m; cin >> n >> m; double sum = 0.0; for (int i = 1; i <= m; ++i) cin >> a[i], sum += a[i]; for (int i = 1; i <= n; ++i) { string temp; cin >> temp; double sc = 0.0; for (int j = 1; j <= m; ++j) { for (int k = 1; k <= 3; ++k) cin >> b[k]; sc += (b[1] * 0.5 + b[2] * 0.3 + b[3] * 0.2) * a[j]; } p.push_back(Node{temp, sc / sum}); } stable_sort(p.begin(), p.end(), cmp); for (int i = 0; i < n; ++i) { cout << p[i].id; printf(" %.2f\n", p[i].score); } if (T) printf("------\n"); } return 0; }