Table of Contents
Algorithm
genAllBinEqualSumHalf(n, left, right, diff)
Example
输出
Home Backend Development C++ For all possible binary numbers of length n, the sum of the two halves is equal?

For all possible binary numbers of length n, the sum of the two halves is equal?

Sep 03, 2023 pm 01:21 PM
binary number length and equal

For all possible binary numbers of length n, the sum of the two halves is equal?

Here we will see all possible binary numbers of n bit (n is given by the user) where the sum of each half is same. For example, if the number is 10001 here 10 and 01 are same because their sum is same, and they are in the different halves. Here we will generate all numbers of that type.

Algorithm

genAllBinEqualSumHalf(n, left, right, diff)

left and right are initially empty, diff is holding difference between left and right

Begin
   if n is 0, then
      if diff is 0, then
         print left + right
      end if
      return
   end if
   if n is 1, then
      if diff is 0, then
         print left + 0 + right
         print left + 1 + right
      end if
      return
   end if
   if 2* |diff| <= n, then
      if left is not blank, then
         genAllBinEqualSumHalf(n-2, left + 0, right + 0, diff)
         genAllBinEqualSumHalf(n-2, left + 0, right + 1, diff-1)
      end if
      genAllBinEqualSumHalf(n-2, left + 1, right + 0, diff + 1)
      genAllBinEqualSumHalf(n-2, left + 1, right + 1, diff)
   end if
End
Copy after login

Example

#include <bits/stdc++.h>
using namespace std;
//left and right strings will be filled up, di will hold the difference between left and right
void genAllBinEqualSumHalf(int n, string left="", string right="", int di=0) {
   if (n == 0) { //when the n is 0
      if (di == 0) //if diff is 0, then concatenate left and right
         cout << left + right << " ";
      return;
   }
   if (n == 1) {//if 1 bit number is their
      if (di == 0) { //when difference is 0, generate two numbers one with 0 after left, another with 1 after left, then add right
         cout << left + "0" + right << " ";
         cout << left + "1" + right << " ";
      }
      return;
   }
   if ((2 * abs(di) <= n)) {
      if (left != ""){ //numbers will not start with 0
         genAllBinEqualSumHalf(n-2, left+"0", right+"0", di);
         //add 0 after left and right
         genAllBinEqualSumHalf(n-2, left+"0", right+"1", di-1);
         //add 0 after left, and 1 after right, so difference is 1 less
      }
      genAllBinEqualSumHalf(n-2, left+"1", right+"0", di+1); //add 1 after left, and 0 after right, so difference is 1 greater
      genAllBinEqualSumHalf(n-2, left+"1", right+"1", di); //add 1 after left and right
   }
}
main() {
   int n = 5;
   genAllBinEqualSumHalf(n);
}
Copy after login

输出

100001
100010
101011
110011
100100
101101
101110
110101
110110
111111
Copy after login

The above is the detailed content of For all possible binary numbers of length n, the sum of the two halves is equal?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How many binary digits does an IP address consist of? How many binary digits does an IP address consist of? Mar 01, 2023 pm 04:35 PM

An IP address is composed of 32 or 128-bit binary numbers. IP address is a unified address format provided by the IP protocol. There are two types of IP addresses: 1. IPv4 address, consisting of 32-bit binary numbers, expressed in dotted decimal notation, divided into eight bits, that is, four 0~255 Decimal number; 2. IPv6 address consists of 128-bit binary numbers, expressed in dotted hexadecimal, divided into eight bits, that is, sixteen hexadecimal numbers from 0x00 to 0xff.

Find the 2's complement of a given binary number using C Find the 2's complement of a given binary number using C Sep 05, 2023 pm 03:21 PM

Consider the example given below - The example input is as follows: Input binary number: 10010001 The output is as follows: 1's complement of 10010001 is 011011102 The complement of 10010001 is 01101111 Algorithm Reference to an algorithm to find the 2'c's complement of a given binary number number. Step 1 - Get started. Step 2 - Read the binary number at runtime. Step 3 - Copy the binary number to strdp. Step 4-len:=strlen(str) Step 5-For i=0 to len-1, execute Step 5.1-If str[i]=='1', then Step 5.1.1-str[i]==' 0' Step 5.2-Otherwise Step 5.2.1

What is the PHP array length limit? What is the PHP array length limit? Mar 13, 2024 pm 06:30 PM

There is no fixed limit to the length of an array in PHP, it can be dynamically adjusted according to the system's memory size. In PHP, an array is a very flexible data structure that can store any number of elements, and each element can be a value of any type, or even another array. The length limit of PHP arrays mainly depends on the memory size of the system and the memory limit of PHP configuration. Generally speaking, if the system's memory is large enough and PHP's memory limit is high enough, the length of the array can be very large. However, if your system is low on memory or

Is there a limit to PHP array length? Is there a limit to PHP array length? Mar 13, 2024 pm 06:36 PM

Is there a limit to PHP array length? Need specific code examples In PHP, the array length is not subject to a fixed limit, and the array size can be dynamically adjusted according to the actual limit of the system memory. Arrays in PHP are dynamic arrays, so they can grow or shrink dynamically as needed. In PHP, an array is an ordered mapped data structure, and array elements can be accessed using array subscripts or associative array key values. Let's look at a specific code example to demonstrate whether the PHP array length is limited. First, we can pass the following code

What are the 1's complement and 2's complement of binary numbers? What are the 1's complement and 2's complement of binary numbers? Sep 11, 2023 pm 11:33 PM

Binary numbers are represented in base 2. It only uses the two digits "0" and "1". Each digit in a binary number is a bit. Example The two's complement number of the binary number -01000101111 The two's complement number is obtained by reversing the digits of the binary number, that is, converting 1 to 0 and 0 to 1. The complement of example 1’sComplementof101100=0100112 is the complement of the binary number plus 1, that is, the complement of 1 + 1. Example 2’scomplementof101101is010011. Example code to find one and two complement codes - #include<iostr

Given an array, find the maximum sum of the lengths of two strings that do not have the same characters. Given an array, find the maximum sum of the lengths of two strings that do not have the same characters. Aug 29, 2023 pm 06:45 PM

The purpose of this article is to implement a program that maximizes the sum of the lengths of a pair of strings that have no common characters in a given array. By definition, a string is a collection of characters. Problem Statement Implement a program to maximize the sum of lengths of a pair of strings that have no common characters in a given array. Example 1LetusconsidertheInputarray:a[]=["efgh","hat","fto","car","wxyz","fan"]Outputobtained:8 Description There are no common characters in the strings "abcd" and "wxyz". As a result, the combined length of the two strings is 4+4, which is equal to 8, which is the longest length among all feasible pairs. Example 2Letu

How many binary digits does an IPv6 address consist of? How many binary digits does an IPv6 address consist of? Dec 15, 2020 pm 02:02 PM

IPv6 addresses are composed of 128-bit binary numbers. IPv6 addresses are binary numbers represented in hexadecimal and have a 128-bit address length. An IPv6 IP address consists of 8 address sections, each section contains 16 address bits, and the total length is 16x8=128 bits.

Multiple perspectives on the use and importance of the len function Multiple perspectives on the use and importance of the len function Dec 28, 2023 am 08:38 AM

The role and meaning of the len function is interpreted from different angles. The len function is one of the commonly used functions in the Python programming language. It is mainly used to return the length or number of elements of a container object (such as string, list, tuple, etc.). This simple function plays a very important role when writing programs, and its function and meaning can be interpreted from many angles. This article will explain the len function from the perspectives of performance, readability, and container type, and provide specific code examples. 1. Performance perspective When processing large-scale data, the performance of the program

See all articles