📌  相关文章
📜  查找具有相同的前半部分和后半部分总和的所有偶数长度的二进制序列|迭代式

📅  最后修改于: 2021-05-04 13:12:31             🧑  作者: Mango

给定数字N ,找到所有长度为2 * N的二进制序列,以使前N个位的总和与后N个位的总和相同。

例子:

注意:可在此处找到针对此问题的递归方法。
方法:
一种简单的方法来运行从02 2 * N的循环并转换为二进制形式,并检查上半部分的总和是否等于下半部分的总和。

如果上述条件成立,则打印该号码,否则检查下一个号码。

下面是上述方法的实现:

C++
// C++ implementation
#include 
#include 
#include 
using namespace std;
  
// Function to convert the
// number into binary and
// store the number into
// an array
void convertToBinary(int num, int a[], int n)
{
  
    int pointer = n - 1;
    while (num > 0) {
        a[pointer] = num % 2;
        num = num / 2;
        pointer--;
    }
}
  
// Function to check if the
// sum of the digits till
// the mid of the array and
// the sum of the digits
// from mid till n is the
// same, if they are same
// then print that binary
void checkforsum(int a[], int n)
{
  
    int sum1 = 0;
    int sum2 = 0;
    int mid = n / 2;
  
    // Calculating the sum from
    // 0 till mid and store
    // in sum1
    for (int i = 0; i < mid; i++)
        sum1 = sum1 + a[i];
  
    // Calculating the sum
    // from mid till n and
    // store in sum2
    for (int j = mid; j < n; j++)
        sum2 = sum2 + a[j];
  
    // If sum1 is same as
    // sum2 print the binary
    if (sum1 == sum2) {
        for (int i = 0; i < n; i++)
            cout << a[i];
        cout << "\n";
    }
}
  
// Function to print sequence
void print_seq(int m)
{
  
    int n = (2 * m);
  
    // Creating the array
    int a[n];
  
    // Initialize the array
    // with 0 to store the
    // binary nnumbers
    for (int j = 0; j < n; j++) {
        a[j] = 0;
    }
  
    for (int i = 0; i < (int)pow(2, n); i++) {
  
        // Converting the number
        // into binary first
        convertToBinary(i, a, n);
  
        // Checking if the sum of
        // the first half of the
        // array is same as the
        // sum of the next half
        checkforsum(a, n);
    }
}
  
// Driver Code
int main()
{
    int m = 2;
  
    print_seq(m);
  
    return 0;
}


Java
// Java code implementation for above approach
class GFG
{
      
    // Function to convert the 
    // number into binary and 
    // store the number into 
    // an array 
    static void convertToBinary(int num,
                                int a[], int n) 
    { 
        int pointer = n - 1; 
        while (num > 0) 
        { 
            a[pointer] = num % 2; 
            num = num / 2; 
            pointer--; 
        } 
    } 
      
    // Function to check if the 
    // sum of the digits till 
    // the mid of the array and 
    // the sum of the digits 
    // from mid till n is the 
    // same, if they are same 
    // then print that binary 
    static void checkforsum(int a[], int n) 
    { 
        int sum1 = 0; 
        int sum2 = 0; 
        int mid = n / 2; 
      
        // Calculating the sum from 
        // 0 till mid and store 
        // in sum1 
        for (int i = 0; i < mid; i++) 
            sum1 = sum1 + a[i]; 
      
        // Calculating the sum 
        // from mid till n and 
        // store in sum2 
        for (int j = mid; j < n; j++) 
            sum2 = sum2 + a[j]; 
      
        // If sum1 is same as 
        // sum2 print the binary 
        if (sum1 == sum2)
        { 
            for (int i = 0; i < n; i++) 
                System.out.print(a[i]); 
            System.out.println(); 
        } 
    } 
      
    // Function to print sequence 
    static void print_seq(int m) 
    { 
      
        int n = (2 * m); 
      
        // Creating the array 
        int a[] = new int[n]; 
      
        // Initialize the array 
        // with 0 to store the 
        // binary nnumbers 
        for (int j = 0; j < n; j++) 
        { 
            a[j] = 0; 
        } 
      
        for (int i = 0; i < (int)Math.pow(2, n); i++) 
        { 
      
            // Converting the number 
            // into binary first 
            convertToBinary(i, a, n); 
      
            // Checking if the sum of 
            // the first half of the 
            // array is same as the 
            // sum of the next half 
            checkforsum(a, n); 
        } 
    } 
      
    // Driver Code 
    public static void main (String[] args)
    { 
        int m = 2; 
      
        print_seq(m); 
    } 
}
  
// This code is contributed by AnkitRai01


Python3
# Python3 implementation of above approach
  
# Function to convert the number into binary 
# and store the number into an array
def convertToBinary(num, a, n):
  
    pointer = n - 1
    while (num > 0):
        a[pointer] = num % 2
        num = num // 2
        pointer -= 1
  
# Function to check if the sum of the digits till
# the mid of the array and the sum of the digits
# from mid till n is the same, if they are same
# then print that binary
def checkforsum(a, n):
  
    sum1 = 0
    sum2 = 0
    mid = n // 2
  
    # Calculating the sum from 0 till mid 
    # and store in sum1
    for i in range(mid):
        sum1 = sum1 + a[i]
  
    # Calculating the sum from mid till n 
    # and store in sum2
    for j in range(mid, n):
        sum2 = sum2 + a[j]
  
    # If sum1 is same as sum2 print the binary
    if (sum1 == sum2):
        for i in range(n):
            print(a[i], end = "")
        print()
  
# Function to prsequence
def print_seq(m):
  
    n = (2 * m)
  
    # Creating the array
    a = [0 for i in range(n)]
  
  
    for i in range(pow(2, n)):
  
        # Converting the number
        # into binary first
        convertToBinary(i, a, n)
  
        # Checking if the sum of the first half 
        # of the array is same as the sum of 
        # the next half
        checkforsum(a, n)
  
# Driver Code
m = 2
  
print_seq(m)
  
# This code is contributed by mohit kumar


C#
// C# code implementation for above approach
using System;
      
class GFG
{
      
    // Function to convert the 
    // number into binary and 
    // store the number into 
    // an array 
    static void convertToBinary(int num,
                                int []a, int n) 
    { 
        int pointer = n - 1; 
        while (num > 0) 
        { 
            a[pointer] = num % 2; 
            num = num / 2; 
            pointer--; 
        } 
    } 
      
    // Function to check if the 
    // sum of the digits till 
    // the mid of the array and 
    // the sum of the digits 
    // from mid till n is the 
    // same, if they are same 
    // then print that binary 
    static void checkforsum(int []a, int n) 
    { 
        int sum1 = 0; 
        int sum2 = 0; 
        int mid = n / 2; 
      
        // Calculating the sum from 
        // 0 till mid and store 
        // in sum1 
        for (int i = 0; i < mid; i++) 
            sum1 = sum1 + a[i]; 
      
        // Calculating the sum 
        // from mid till n and 
        // store in sum2 
        for (int j = mid; j < n; j++) 
            sum2 = sum2 + a[j]; 
      
        // If sum1 is same as 
        // sum2 print the binary 
        if (sum1 == sum2)
        { 
            for (int i = 0; i < n; i++) 
                Console.Write(a[i]); 
            Console.WriteLine(); 
        } 
    } 
      
    // Function to print sequence 
    static void print_seq(int m) 
    { 
      
        int n = (2 * m); 
      
        // Creating the array 
        int []a = new int[n]; 
      
        // Initialize the array 
        // with 0 to store the 
        // binary nnumbers 
        for (int j = 0; j < n; j++) 
        { 
            a[j] = 0; 
        } 
      
        for (int i = 0; i < (int)Math.Pow(2, n); i++) 
        { 
      
            // Converting the number 
            // into binary first 
            convertToBinary(i, a, n); 
      
            // Checking if the sum of 
            // the first half of the 
            // array is same as the 
            // sum of the next half 
            checkforsum(a, n); 
        } 
    } 
      
    // Driver Code 
    public static void Main (String[] args)
    { 
        int m = 2; 
      
        print_seq(m); 
    } 
}
      
// This code is contributed by PrinciRaj1992


输出:
0000
0101
0110
1001
1010
1111