Binary String Constructing
time limit per test 1 second
memory limit per test 256 megabytes
input standard input
output standard output
You are given three integers aa, bb and xx. Your task is to construct a binary string ss of length n=a+bn=a+b such that there are exactly aa zeroes, exactly bb ones and exactly xx indices ii (where 1≤i<n1≤i<n) such that si≠si+1si≠si+1. It is guaranteed that the answer always exists.
For example, for the string "01010" there are four indices ii such that 1≤i<n1≤i<n and si≠si+1si≠si+1 (i=1,2,3,4i=1,2,3,4). For the string "111001" there are two such indices ii (i=3,5i=3,5).
Recall that binary string is a non-empty sequence of characters where each character is either 0 or 1.
Input
The first line of the input contains three integers aa, bb and xx (1≤a,b≤100,1≤x<a+b)1≤a,b≤100,1≤x<a+b).
Output
Print only one string ss, where ss is any binary string satisfying conditions described above. It is guaranteed that the answer always exists.
Examples
input
2 2 1
output
1100
input
3 3 3
output
101100
input
5 3 6
output
01010100
Note
All possible answers for the first example:
- 1100;
- 0011.
All possible answers for the second example:
- 110100;
- 101100;
- 110010;
- 100110;
- 011001;
- 001101;
- 010011;
- 001011.
题解
题目大致意思是:a个0,b个1,组成一个长为a+b的字符串s,要求有x个位置使得si≠si+1
因为题目不要求格式,只要符合题意就可以。所以可以先放多的,也就是a大先放0,b大先放1,交替放置。需要注意的是,当x为1时要一次性把剩下的0或1全部放置,具体放0还是1,可以设一个标记mark
# include <cstdio>
# include <algorithm>
# include <cstring>
using namespace std;
const int maxn = 200+ 10;
int main()
{
int a, b, x;
scanf("%d%d%d",&a,&b,&x);
int mark;
if(a>=b) mark = 0;
else mark = 1;
while(x)
{
if(x==1)
{
if(mark)
{
for(int i=0;i<b;i++) printf("1");
for(int i=0;i<a;i++) printf("0");
}
else
{
for(int i=0;i<a;i++) printf("0");
for(int i=0;i<b;i++) printf("1");
}
}
else
{
printf("%d",mark);
if(mark)
{
b--;
mark = 0;
}
else
{
a--;
mark = 1;
}
}
x--;
}
printf("\n");
return 0;
}