CodeForces 1003B Binary String Constructing

原题:https://codeforces.com/problemset/problem/1003/B

题意:给你 3 个整数 a ,b ,x ,让你求出一个包含了 a 个 0,b 个 1 的二进制串 s。要求其中 s_{i}\neq s_{i+1}的对数正好等于 x。

思路:首先要凑够x对s_{i}\neq s_{i+1},思考易得在[0~x]中交替填入1和0刚好能有x对,那么剩下的0和1分别在位置0和位置1上填入 并不会影响s_{i}\neq s_{i+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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值