📌  相关文章
📜  通过仅翻转一组连续的 0 来最大化十进制等效值

📅  最后修改于: 2021-09-03 03:17:14             🧑  作者: Mango

给定一个字符串形式的二进制数,任务是打印通过仅翻转一组连续的 0 获得的二进制等效值,使得该二进制数的十进制等效值最大。
注意:不要假设二进制数的开头有任何尾随零,即“0101”表示为“101”。
例子:

方法:为了解决上面提到的问题,我们知道我们必须增加二进制等价物的值。因此,我们必须在较高位置增加 1 的数量。显然,我们可以通过只翻转最初出现的零来增加数字的值。遍历字符串并翻转第一次出现的零,直到出现 1,在这种情况下,循环必须中断。打印结果字符串。
下面是上述方法的实现:

C++
// C++ implementation to Maximize the value of
// the decimal equivalent given in the binary form
#include 
using namespace std;
 
// Function to print the binary number
void flip(string& s)
{
    for (int i = 0; i < s.length(); i++) {
 
        // Check if the current number is 0
        if (s[i] == '0') {
 
            // Find the continuous 0s
            while (s[i] == '0') {
 
                // Replace initially
                // occurring 0 with 1
                s[i] = '1';
                i++;
            }
 
            // Break out of loop if 1 occurs
            break;
        }
    }
}
 
// Driver code
int main()
{
    string s = "100010001";
    flip(s);
 
    cout << s;
    return 0;
}


Java
// Java implementation to maximize the value of
// the decimal equivalent given in the binary form
import java.util.*;
 
class GFG{
 
// Function to print the binary number
static void flip(String s)
{
    StringBuilder sb = new StringBuilder(s);
    for(int i = 0; i < sb.length(); i++)
    {
        
       // Check if the current number is 0
       if (sb.charAt(i) == '0')
       {
            
           // Find the continuous 0s
           while (sb.charAt(i) == '0')
           {
                
               // Replace initially
               // occurring 0 with 1
               sb.setCharAt(i, '1');
               i++;
           }
            
           // Break out of loop if 1 occurs
           break;
       }
    }
    System.out.println(sb.toString());
}
 
// Driver code
public static void main(String[] args)
{
    String s = "100010001";
    flip(s);
}
}
 
// This code is contributed by offbeat


Python3
# Python3 implementation to
# Maximize the value of the
# decimal equivalent given
# in the binary form
 
# Function to print the binary
# number
def flip(s):
    s = list(s)
    for i in range(len(s)):
 
        # Check if the current number
        # is 0
        if(s[i] == '0'):
 
            # Find the continuous 0s
            while(s[i] == '0'):
 
                # Replace initially
                # occurring 0 with 1
                s[i] = '1'
                i += 1
            s = ''.join(map(str, s))
 
            # return the string and
            # break the loop
            return s
 
# Driver code
s = "100010001"
print(flip(s))
 
# This code is contributed by avanitrachhadiya2155


C#
// C# implementation to maximize the value of
// the decimal equivalent given in the binary form
using System;
 
class GFG{
 
// Function to print the binary number
static String flip(char []s)
{
    for(int i = 0; i < s.Length; i++)
    {
        
       // Check if the current number is 0
       if (s[i] == '0')
       {
            
           // Find the continuous 0s
           while (s[i] == '0')
           {
                
               // Replace initially
               // occurring 0 with 1
               s[i] = '1';
               i++;
           }
            
           // Break out of loop if 1 occurs
           break;
       }
    }
    return new String(s);
}
 
// Driver code
public static void Main(String[] args)
{
    String s = "100010001";
     
    Console.WriteLine(flip(s.ToCharArray()));
}
}
 
// This code is contributed by Rohit_ranjan


输出:
111110001

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live