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
这种题不该写blog的,但是打比赛的时候我翻译不出来题意,赛后还wa了几发,我真的好菜啊!时刻提醒自己
思路:让0和1排序使得出现个数为x的s[i]!=s[i-1]。于是只需要让个数多的先排在前面不断交替x-1次,最后把余下的0和1都输出就完事。
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<iostream>
#include<math.h>
#include<algorithm>
using namespace std;
int main()
{
int sum[300];
int a,b,x,p,q,t,k,i;
scanf("%d %d %d",&a,&b,&x);
p=a,q=b;
k=0;
if(a<=b)
{
sum[1]=1;
b--;
}
else
{
sum[1]=0;
a--;
}
for(i=2; i<=p+q; i++)
{
if(k==x-1)
{
break;
}
if(sum[i-1]==1)
{
sum[i]=0;
a--;
k++;
}
else if(sum[i-1]==0)
{
sum[i]=1;
b--;
k++;
}
}
t=i;
if(sum[i-1]==0)
{
for(;a>0;)
{
sum[t++]=0;
a--;
}
for(;b>0;)
{
sum[t++]=1;
b--;
}
}
else if(sum[i-1]==1)
{
for(;b>0;)
{
sum[t++]=1;
b--;
}
for(;a>0;)
{
sum[t++]=0;
a--;
}
}
for(i=1; i<=p+q; i++)
printf("%d",sum[i]);
}