A.字符统计
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
string s;
cin >> t;
getchar();
while (t--) {
int duan = 0, word = 0, cntchar = 0;
while (getline(cin, s), s != "=====") {
if (cin.eof())
break;
cntchar += s.size();
duan++;
int cnt = 0, jd = 0;
for (int i = 0; i < s.size(); i++) {
if (s[i] != ' ') {
jd = 1;
} else {
if (jd)
cnt++, jd = 0;
else
jd = 0;
}
}
word += cnt + jd;
}
printf("%d %d %d\n", duan, word, cntchar);
}
return 0;
}
B.连分数
#include <bits/stdc++.h>
using namespace std;
int n, a, b, ta, tb;
void dfs(int a, int b) {
cout << "+1/";
if (a == 1) {
cout << b;
return;
} else if (a != 1) {
cout << "{";
int k = b / a;
cout << k;
if (b % a != 0) {
dfs(b % a, a);
}
cout << "}";
}
}
inline int gcd(int a, int b) {
if (a < b) {
swap(a, b);
}
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
int main() {
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> a >> b;
int k = a / b;
cout << a << "/" << b << " = " << k;
int p = gcd(a, b);
a /= p, b /= p;
if (a % b != 0) {
dfs(a % b, b);
}
cout << endl;
}
return 0;
}
C.挪酒瓶
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
//v数组为1这表示酒瓶已到达正确位置,p数组记录要去的位置,w数组表示酒瓶重量
int p[100005], w[100005], v[100005];
int main() {
int T;
scanf("%d", &T);
while (T--) {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &p[i]);
v[i] = 0;
if (p[i] == i)
v[i] = 1;
}
int m1 = 105;
ll sum = 0;
for (int i = 1; i <= n; i++) {
scanf("%d", &w[i]);
m1 = min(m1, w[i]);
}
for (int i = 1; i <= n; i++) {
if (v[i])
continue;
int x = i, m2 = 105, cnt = 0;
while (!v[x]) {
v[x] = 1;
sum += w[x];
m2 = min(m2, w[x]);
x = p[x];
cnt++;
}
sum +=
min(m2 * (cnt - 2), (cnt - 2) * m1 + 2 * (m1 + m2) - m2 + m1);
}
cout << sum << endl;
}
}
题解: