


This tutorial will discuss representing a number as the smallest sum of pseudo-binary numbers. A pseudo-binary number is a number composed of the binary digits 0 and 1. Examples of pseudo-binary numbers are 00, 11, 10, 100, 111, 1011, etc.
Here are some examples of numbers represented as sums of pseudo-binary numbers.
Input : 23 Output : 11 + 11 + 1 Explanation : 23 = 11 + 11 + 1, sum of pseudo-binary numbers(11, 11, 1) is 23. Input : 50 Output : 10 + 10 + 10 + 10 + 10
Methods to find the solution
The following is one of the best ways to find the smallest pseudo-binary number that represents N.
Take a number X and update the number of digits in X to 1 or 0 according to each digit of the number N.
-
Check each digit of N:
If it is 0, set that bit of X to 0.
If it is not 0, set the bit of X to 1.
Assuming N = 32, X will become 11.
# Then X will become a pseudo-binary number.
Now subtract X from N and repeat step 1 until N becomes zero.
Example
C code for the above method
#include<iostream> using namespace std; int main(){ int N = 51; // find a pseudo-binary number until N becomes 0. cout << "pseudo-binary representation of " << N << " is: "; while (N > 0){ // finding X which contains 0's and 1's according to N. int temp = N; int X = 0, bit = 1; // checking each place of N for zero or non-zero. while (temp!=0){ int last_dig = temp % 10; temp = temp / 10; if (last_dig != 0) X += bit; bit *= 10; } // printing one pseudo-binary number. cout << X << " "; // Updating N by subtracting with X. N = N - X; } return 0; }
Output
pseudo-binary representation of 51 is: 11 10 10 10 10
Understanding the code
An outer while loop is used to get N and select the number at each position to find X.
We do this by updating the value of N into the temp variable and using an inner loop to check each position of the temp variable and update that position of the variable X.
Print the value of X because it is a pseudo-binary number.
We update N by subtracting X from N and entering the outer loop again until N becomes 0.
Conclusion
In this tutorial, we discussed how to represent a number as the smallest possible sum of pseudo-binary numbers. We discussed ways to find all pseudo-binary numbers. We also discussed that the same C code we can write in other programming languages like C, Java, Python, etc. Hope you find this tutorial helpful.
The above is the detailed content of In C++, represent a number as the smallest possible sum of pseudo-binary numbers. For more information, please follow other related articles on the PHP Chinese website!

我们都知道不是任何数字的平方的数字,如2、3、5、7、8等。非平方数有N个,不可能知道每个数字。因此,在本文中,我们将解释有关无平方数或非平方数的所有内容,以及在C++中查找第N个非平方数的方法。第N个非平方数如果一个数是整数的平方,则该数被称为完全平方数。完全平方数的一些例子是-1issquareof14issquareof29issquareof316issquareof425issquareof5如果一个数不是任何整数的平方,则该数被称为非平方数。例如,前15个非平方数是-2,3,5,6,

在本文中,我们将了解逆转算法,将给定的数组向右旋转k个元素,例如−Input:arr[]={4,6,2,6,43,7,3,7},k=4Output:{43,7,3,7,4,6,2,6}Explanation:Rotatingeachelementofarrayby4-elementtotherightgives{43,7,3,7,4,6,2,6}.Input:arr[]={8,5,8,2,1,4,9,3},k=3Output:{4,9,3,8,5,8,2,1}寻找解决方案的方

圆是封闭图形。圆上的所有点到圆内一点的距离都相等。中心点称为圆心。点到圆心的距离称为半径。面积是封闭图形尺寸跨度的定量表示。圆的面积是圆的尺寸内包围的面积。计算圆面积的公式,Area=π*r*r为了计算面积,我们给出了圆的半径作为输入,我们将使用公式来计算面积,算法STEP1:Takeradiusasinputfromtheuserusingstdinput.STEP2:Calculatetheareaofcircleusing, area=(

我们需要适当的知识才能在C++的数组语法中创建几个唯一的对。在查找唯一对的数量时,我们计算给定数组中的所有唯一对,即可以形成所有可能的对,其中每个对应该是唯一的。例如-Input:array[]={5,5,9}Output:4Explanation:Thenumberofalluniquepairsare(5,5),(5,9),(9,5)and(9,9).Input:array[]={5,4,3,2,2}Output:16寻找解决方案的方法有两种方法可以解决这个问题,它们是−

在本文中,我们将使用C++解决寻找最大值和最小值相同的子数组数量的问题。以下是该问题的示例−Input:array={2,3,6,6,2,4,4,4}Output:12Explanation:{2},{3},{6},{6},{2},{4},{4},{4},{6,6},{4,4},{4,4}and{4,4,4}arethesubarrayswhichcanbeformedwithmaximumandminimumelementsame.Input:array={3,3,1,5,

在这个问题中,我们得到一个指向链表头部的指针和一个整数k。在大小为k的组中,我们需要反转链表。例如-Input:1<->2<->3<->4<->5(doublylinkedlist),k=3Output:3<->2<->1<->5<->4寻找解决方案的方法在这个问题中,我们将制定一个递归算法来解决这个问题。在这种方法中,我们将使用递归并使用递归来解决问题。示例#include<iostream&

在本文中,我们将解释在一个集合上找到反身关系的方法。在这个问题中,我们给出一个数字n,以及一个由n个自然数组成的集合,我们必须确定反身关系的数量。反身关系-如果对于集合A中的每个'a',(a,a)属于关系R,则称关系R是集合A上的反身关系。例如-Input:x=1Output:1Explanation:set={1},reflexiverelationsonA*A:{{1}}Input:x=2Output:4Explanation:set={1,2},reflexiverelationsonA*

在给定的问题中,我们有一个数组,并且我们需要使用反转算法将数组旋转d个元素,例如−Input:arr[]=[1,2,3,4,5,6,7],d=2Output:arr[]=[3,4,5,6,7,1,2]Explanation:Asyoucanseewehavetorotatethisarraybyd=2butourmaintaskistoachievethisbyusingareversaltechnique.我们对数组的旋转进行了一些反转技术的计算,并得出结论:首先,我们反转


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version
Chinese version, very easy to use

Dreamweaver Mac version
Visual web development tools