CF1003B Binary String Constructing

题目描述

You are given three integers a a a , b b b and x x x . Your task is to construct a binary string s s s of length n=a+b n = a + b n=a+b such that there are exactly a a a zeroes, exactly b b b ones and exactly x x x indices i i i (where 1≤i<n 1 \le i < n 1≤i<n ) such that si≠si+1 s_i \ne s_{i + 1} si​≠si+1​ . It is guaranteed that the answer always exists.

For example, for the string "01010" there are four indices i i i such that 1≤i<n 1 \le i < n 1≤i<n and si≠si+1 s_i \ne s_{i + 1} si​≠si+1​ ( i=1,2,3,4 i = 1, 2, 3, 4 i=1,2,3,4 ). For the string "111001" there are two such indices i i i ( i=3,5 i = 3, 5 i=3,5 ).

Recall that binary string is a non-empty sequence of characters where each character is either 0 or 1.

输入输出格式

输入格式:

 

The first line of the input contains three integers a a a , b b b and x x x ( 1≤a,b≤100,1≤x<a+b) 1 \le a, b \le 100, 1 \le x < a + b) 1≤a,b≤100,1≤x<a+b) .

 

输出格式:

 

Print only one string s s s , where s s s is any binary string satisfying conditions described above. It is guaranteed that the answer always exists.

 

输入输出样例

输入样例#1: 复制

2 2 1

输出样例#1: 复制

1100

输入样例#2: 复制

3 3 3

输出样例#2: 复制

101100

输入样例#3: 复制

5 3 6

输出样例#3: 复制

01010100

说明

All possible answers for the first example:

  • 1100;
  • 0011.

All possible answers for the second example:

  • 110100;
  • 101100;
  • 110010;
  • 100110;
  • 011001;
  • 001101;
  • 010011;
  • 001011.

思路:对于x,x/2对0和1(n对0和1交错出现可以提供2*n-1个符合题意的ai),然后将剩余的0和剩余的1连续输出(提供1个符合条件的ai)若X为偶数输出顺序与原来相同,否则相反,这样就刚好是x个符合条件的ai了,需要注意的是,我们要优先将个数多的放在前面,例如,有10个1,5个0的话,我们先输出x/2个“10”,否则,输出x/2个“01”)

   


#include "iostream"

using namespace std;

int main()

{

    int a,b,x,n;

    char u,v;

    cin>>a>>b>>x;

    n=a+b;

    if(a>b) u='0',v='1';

    else {u='1',v='0';swap(a,b);}

    for(int i=0;i<x/2;i++) {cout<<u<<v;a--,b--;}

    if(x%2==0){

        while(b--) cout<<v;

        while(a--) cout<<u;

    }

    else{

        while(a--) cout<<u;

        while(b--) cout<<v;

    }

    return 0;

}

 

 

以上代码非本人所写,

下面的代码存在错输暂存

#include<cstdio>
#include<cmath>
#include<algorithm>
#include<string>
#include<iostream>
using namespace std;
int a[2],x;
int main()
{
    string str="";
    scanf("%d%d%d",&a[0],&a[1],&x);
        if(x%2==0)
        {
            if(a[0]<a[1])
            {
                while(a[1]--)
                {
                    a[0]--;
                    str=str+"1";
                    if(a[0]>=0)
                      str=str+"0";
                }
            }
            else if(a[0]>=a[1])
            {
                while((a[0]--)>=0)
                {
                     a[1]--;
                    if(a[0]==a[1]&&a[1]==0)
                    {
                        str=str+"1";
                        str=str+"0";
                        break;
                    }
                    if(a[0]>=0)
                    str=str+"0";
                    if(a[1]>=0)
                    str=str+"1";
                }
            }

        }
        else
        {

            if(a[0]<a[1])
            {
                while(a[0]--||a[1]--)
                {
                    str=str+"1";
                    if(a[0]==1)
                    {
                        str=str+"1";
                        a[1]--;
                    }
                    if(a[0]>=0)
                      str=str+"0";
                }
            }
            else if(a[0]>=a[1])
            {
                while((a[0]--)>=0)
                {
                     a[1]--;
                     if(a[0]>0)
                    str=str+"0";
                    if(a[1]==1)
                    {
                        str=str+"0";
                        a[0]--;
                    }
                    if(a[1]>=0)
                    str=str+"1";
                }
            }

        }
     cout<<str<<endl;
        return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不会敲代码的小帅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值