📌  相关文章
📜  按照给定的约束构造一个二进制字符串

📅  最后修改于: 2021-06-26 12:57:57             🧑  作者: Mango

给定三个整数ABX。任务是构造一个二进制字符串str ,该字符串的正好是A0B1,前提是必须至少有X个索引,这样str [i]!= str [i + 1] 。输入的信息总是有一个有效的解决方案。
例子:

方法:

  • x除以2并将其存储在变量d中
  • 检查d是否为偶数,并且d / 2!= a ,如果条件为true,则打印0并将da1
  • 1循环到d并打印10 ,最后更新a = a – db = b – d
  • 最后根据ab的值打印其余的01

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to print a binary string which has
// 'a' number of 0's, 'b' number of 1's and there are
// at least 'x' indices such that s[i] != s[i+1]
int constructBinString(int a, int b, int x)
{
    int d, i;
 
    // Divide index value by 2 and store
    // it into d
    d = x / 2;
 
    // If index value x is even and
    // x/2 is not equal to a
    if (x % 2 == 0 && x / 2 != a) {
        d--;
        cout << 0;
        a--;
    }
 
    // Loop for d for each d print 10
    for (i = 0; i < d; i++)
        cout << "10";
 
    // subtract d from a and b
    a = a - d;
    b = b - d;
 
    // Loop for b to print remaining 1's
    for (i = 0; i < b; i++) {
        cout << "1";
    }
 
    // Loop for a to print remaining 0's
    for (i = 0; i < a; i++) {
        cout << "0";
    }
}
 
// Driver code
int main()
{
    int a = 4, b = 3, x = 2;
    constructBinString(a, b, x);
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
// Function to print a binary string which has
// 'a' number of 0's, 'b' number of 1's and there are
// at least 'x' indices such that s[i] != s[i+1]
static void constructBinString(int a, int b, int x)
{
    int d, i;
 
    // Divide index value by 2 and store
    // it into d
    d = x / 2;
 
    // If index value x is even and
    // x/2 is not equal to a
    if (x % 2 == 0 && x / 2 != a)
    {
        d--;
        System.out.print("0");
        a--;
    }
 
    // Loop for d for each d print 10
    for (i = 0; i < d; i++)
        System.out.print("10");
 
    // subtract d from a and b
    a = a - d;
    b = b - d;
 
    // Loop for b to print remaining 1's
    for (i = 0; i < b; i++)
    {
        System.out.print("1");
    }
 
    // Loop for a to print remaining 0's
    for (i = 0; i < a; i++)
    {
        System.out.print("0");
    }
}
 
// Driver code
public static void main(String[] args)
{
    int a = 4, b = 3, x = 2;
    constructBinString(a, b, x);
}
}
 
// This code is contributed
// by Mukul Singh


Python3
# Python3 implementation of the above approach
 
# Function to print a binary string which
# has 'a' number of 0's, 'b' number of 1's
# and there are at least 'x' indices such
# that s[i] != s[i+1]
def constructBinString(a, b, x):
 
    # Divide index value by 2 and
    # store it into d
    d = x // 2
 
    # If index value x is even and
    # x/2 is not equal to a
    if x % 2 == 0 and x // 2 != a:
        d -= 1
        print("0", end = "")
        a -= 1
 
    # Loop for d for each d print 10
    for i in range(d):
        print("10", end = "")
 
    # subtract d from a and b
    a = a - d
    b = b - d
 
    # Loop for b to print remaining 1's
    for i in range(b):
        print("1", end = "")
     
    # Loop for a to print remaining 0's
    for i in range(a):
        print("0", end = "")
 
# Driver Code
if __name__ == "__main__":
 
    a, b, x = 4, 3, 2
    constructBinString(a, b, x)
 
# This code is contributed by Rituraj_Jain


C#
// C# implementation of the approach
using System;
 
class GFG
{
// Function to print a binary string which has
// 'a' number of 0's, 'b' number of 1's and there are
// at least 'x' indices such that s[i] != s[i+1]
static void constructBinString(int a, int b, int x)
{
    int d, i;
 
    // Divide index value by 2 and store
    // it into d
    d = x / 2;
 
    // If index value x is even and
    // x/2 is not equal to a
    if (x % 2 == 0 && x / 2 != a)
    {
        d--;
        Console.Write("0");
        a--;
    }
 
    // Loop for d for each d print 10
    for (i = 0; i < d; i++)
        Console.Write("10");
 
    // subtract d from a and b
    a = a - d;
    b = b - d;
 
    // Loop for b to print remaining 1's
    for (i = 0; i < b; i++)
    {
        Console.Write("1");
    }
 
    // Loop for a to print remaining 0's
    for (i = 0; i < a; i++)
    {
        Console.Write("0");
    }
}
 
// Driver code
public static void Main()
{
    int a = 4, b = 3, x = 2;
    constructBinString(a, b, x);
}
}
 
// This code is contributed
// by Akanksha Rai


PHP


Javascript


输出:
0111000

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。