T1:格式改写
代码实现
s = input()
cnt = 0
for c in s:
if c.isupper():
cnt += 1
ans = min(cnt, len(s)-cnt)
print(ans)
T2:倍数统计
代码实现
import sys
input = lambda: sys.stdin.readline().rstrip()
a, b = map(int, input().split())
c = int(input())
print(b//c-(a-1)//c)
T3:区间的并
原题:LC 56
代码实现
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using P = pair<int, int>;
int main() {
int n;
cin >> n;
vector<P> intervals;
rep(i, n) {
int a, b;
cin >> a >> b;
intervals.emplace_back(a, b);
}
sort(intervals.begin(), intervals.end());
vector<P> ans;
rep(i, n) {
auto [l, r] = intervals[i];
if (!ans.size() or ans.back().second < l) {
ans.emplace_back(l, r);
}
else {
ans.back().second = max(ans.back().second, r);
}
}
for (auto [a, b] : ans) {
cout << a << ' ' << b << '\n';
}
return 0;
}
T4:平分数字(一)
暴搜
代码实现
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
int main() {
int n;
cin >> n;
vector<int> a(n);
rep(i, n) cin >> a[i];
ll sum = accumulate(a.begin(), a.end(), 0);
if (sum%2 != 0) {
puts("No");
return 0;
}
sum /= 2;
bool ok = false;
auto dfs = [&](auto f, int i, ll now=0) -> void {
if (i == n) return;
if (now == sum) {
ok = true;
return;
}
f(f, i+1, now);
f(f, i+1, now+a[i]);
};
dfs(dfs, 0);
if (ok) puts("Matched");
else puts("No");
return 0;
}