原题:https://codeforces.com/problemset/problem/1003/B
题意:给你 3 个整数 a ,b ,x ,让你求出一个包含了 a 个 0,b 个 1 的二进制串 s。要求其中 的对数正好等于 x。
思路:首先要凑够x对,思考易得在[0~x]中交替填入1和0刚好能有x对,那么剩下的0和1分别在位置0和位置1上填入 并不会影响对数,那么怎么完成01交替填入呢,很简单通过奇偶性来填就能实现了。
AC代码:
#include <bits/stdc++.h>
int cnt[201], ans[201];
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int a, b, x;
std::cin >> a >> b >> x;
if (a > b) {
for (int i = 0; i <= x; i++) {
ans[i] = i & 1, cnt[i] = 1;
if (ans[i]) b--;
else a--;
}
cnt[0] += a;
cnt[1] += b;
} else {
for (int i = 0; i <= x; i++) {
ans[i] = ~i & 1, cnt[i] = 1;
if (ans[i]) b--;
else a--;
}
cnt[0] += b;
cnt[1] += a;
}
for (int i = 0; i <= x; i++) {
for (int j = 0; j < cnt[i]; j++) {
std::cout << ans[i];
}
}
return 0;
}