给定二进制字符串str ,任务是打印通过以下操作将其转换为1所需的步骤数:
- 如果“ S”为奇数,则向其添加1。
- 如果“ S”被2除。
例子:
Input: str = “1001001”
Output: 12
Input: str = “101110”
Output: 8
Number ‘101110’ is even, after dividing it by 2 we get an odd number ‘10111’ so we will add 1 to it. Then we’ll get ‘11000’ which is even and can be divide three times continuously in a row and get ’11’ which is odd, adding 1 to it will give us ‘100’ which is even and can be divided 2 times in a row. As, a result we get 1.
So 8 times the above two operations were required in this number.
以下是解决此问题的分步算法:
- 将字符串S初始化为二进制数。
- 如果二进制文件的大小为1,则所需的操作数将为0。
- 如果最后一位数字为0,则其为偶数,因此需要执行一次运算才能将其除以2。
- 遇到1后,遍历直到达到0,然后对每一位进行一次操作。
- 遍历过程中在1后面遇到0之后,将0替换为1并再次从步骤4开始。
下面是上述算法的实现:
C++
// C++ program to count the steps
// required to convert a number to 1
#include
using namespace std;
#define ll long long
// function to calculate the number of actions
int calculate_(string s)
{
// if the size of binary is 1
// then the number of actions will be zero
if (s.size() == 1)
return 0;
// initializing the number of actions as 0 at first
int count_ = 0;
for (int i = s.length() - 1; i > 0;) {
// start traversing from the last digit
// if its 0 increment the count and decrement i
if (s[i] == '0') {
count_++;
i--;
}
// if s[i] == '1'
else {
count_++;
// stop until you get 0 in the binary
while (s[i] == '1' && i > 0) {
count_++;
i--;
}
if (i == 0)
count_++;
// when encounter a 0 replace it with 1
s[i] = '1';
}
}
return count_;
}
// Driver code
int main()
{
string s;
s = "10000100000";
cout << calculate_(s);
return 0;
}
Java
//Java program to count the steps
//required to convert a number to 1
public class ACX {
//function to calculate the number of actions
static int calculate_(String s)
{
// if the size of binary is 1
// then the number of actions will be zero
if (s.length() == 1)
return 0;
// initializing the number of actions as 0 at first
int count_ = 0;
char[] s1=s.toCharArray();
for (int i = s.length() - 1; i > 0😉 {
// start traversing from the last digit
// if its 0 increment the count and decrement i
if (s1[i] == '0') {
count_++;
i--;
}
// if s[i] == '1'
else {
count_++;
// stop until you get 0 in the binary
while (s1[i] == '1' && i > 0) {
count_++;
i--;
}
if (i == 0)
count_++;
// when encounter a 0 replace it with 1
s1[i] = '1';
}
}
return count_;
}
//Driver code
public static void main(String []args)
{
String s;
s = "10000100000";
System.out.println(calculate_(s));
}
}
Python3
# Python3 program to count the steps
# required to convert a number to 1
# Method to calculate the number of actions
def calculate_(s):
# if the size of binary is 1
# then the number of actions will be zero
if len(s) == 1:
return 0
# initializing the number of actions as 0 at first
count_ = 0
i = len(s) - 1
while i > 0:
# start traversing from the last digit
# if its 0 increment the count and decrement i
if s[i] == '0':
count_ += 1
i -= 1
# if s[i] == '1'
else:
count_ += 1
# stop until you get 0 in the binary
while s[i] == '1' and i > 0:
count_ += 1
i -= 1
if i == 0:
count_ += 1
# when encounter a 0 replace it with 1
s = s[:i] + "1" + s[i + 1:]
return count_
# Driver code
s = "10000100000"
print(calculate_(s))
# This code is contributed by
# Rajnis09
C#
// C# program to count the steps
//required to convert a number to 1
using System;
class GFG
{
// function to calculate the number of actions
static int calculate_(String s)
{
// if the size of binary is 1
// then the number of actions will be zero
if (s.Length == 1)
return 0;
// initializing the number of actions as 0 at first
int count_ = 0;
char[] s1 = s.ToCharArray();
for (int i = s.Length - 1; i > 0;)
{
// start traversing from the last digit
// if its 0 increment the count and decrement i
if (s1[i] == '0')
{
count_++;
i--;
}
// if s[i] == '1'
else
{
count_++;
// stop until you get 0 in the binary
while (s1[i] == '1' && i > 0)
{
count_++;
i--;
}
if (i == 0)
count_++;
// when encounter a 0 replace it with 1
s1[i] = '1';
}
}
return count_;
}
// Driver code
public static void Main(String []args)
{
String s;
s = "10000100000";
Console.WriteLine(calculate_(s));
}
}
// This code is contributed by princiraj1992
PHP
0;)
{
// start traversing from the last
// digit if its 0 increment the
// count and decrement i
if ($s[$i] == '0')
{
$count_++;
$i--;
}
// if $s[$i] == '1'
else
{
$count_++;
// stop until you get 0 in the binary
while ($s[$i] == '1' && $i > 0)
{
$count_++;
$i--;
}
if ($i == 0)
$count_++;
// when encounter a 0 replace
// it with 1
$s[$i] = '1';
}
}
return $count_;
}
// Driver code
$s = "10000100000";
echo calculate_($s);
// This code is contributed
// by Shivi_Aggarwal
?>
Javascript
输出:
16
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。