📜  要删除的最小位数,以使两个连续的位数都不相同

📅  最后修改于: 2021-06-26 12:49:41             🧑  作者: Mango

给定数字N。任务是计算要从数字中删除的最小位数,以便没有两个连续的数字相同。
例子

如果我们只计算连续对等位数的对数,就可以轻松解决该问题。那将是从给定数字中删除的最小数字位数,这样就不会有两个连续的数字相同。
下面是上述方法的实现:

C++
// CPP program to count the minimum number
// of digits to be removed from a number so that
// no two consecutive digits are same
 
#include 
using namespace std;
 
// Function to count the minimum number of digits
// to remove from a number so that no two
// consecutive digits are same.
int countConsecutive(int n)
{
    // convert the number to string
    string s = to_string(n);
 
    // initialize counting variable
    int count = 0;
 
    for (int i = 0; i < s.size() - 1; i++)
        if (s[i] == s[i + 1]) // check if two consecutive digits are same
            count++;
 
    return count;
}
 
// Driver code
int main()
{
    int n = 44522255;
    cout << countConsecutive(n);
    return 0;
}


Java
// Java program to count the minimum
// number of digits to be removed
// from a number so that no two
// consecutive digits are same
import java.lang.*;
import java.util.*;
 
class GFG
{
     
// Function to count the minimum number
// of digits to remove from a number so
// that no two consecutive digits are same.
static int countConsecutive(int n)
{
    // convert the number to string
    String s = Integer.toString(n);
 
    // initialize counting variable
    int count = 0;
 
    for (int i = 0; i < s.length() - 1; i++)
         
        // check if two consecutive
        // digits are same
        if (s.charAt(i) == s.charAt(i + 1))
            count++;
 
    return count;
}
 
// Driver code
public static void main(String args[])
{
    int n = 44522255;
    System.out.println(countConsecutive(n));
}
}
 
// This code is contributed
// by Akanksha Rai(Abby_akku)


Python 3
# Python 3 program to count the
# minimum number of digits to be
# removed from a number so that
# no two consecutive digits are same
 
# Function to count the minimum
# number of digits to remove from
# a number so that no two consecutive
# digits are same.
def countConsecutive(n):
 
    # convert the number to string
    s = str(n)
 
    # initialize counting variable
    count = 0
 
    for i in range(len(s) - 1):
         
        # check if two consecutive
        # digits are same
        if (s[i] == s[i + 1]):
            count += 1
 
    return count
 
# Driver code
if __name__ == "__main__":
    n = 44522255
    print( countConsecutive(n))
 
# This code is contributed
# by ChitraNayal


C#
// C# program to count the minimum
// number of digits to be removed
// from a number so that no two
// consecutive digits are same
using System;
 
class GFG
{
// Function to count the minimum number
// of digits to remove from a number so
// that no two consecutive digits are same.
static int countConsecutive(int n)
{
    // convert the number to string
    string s = n.ToString();
 
    // initialize counting variable
    int count = 0;
 
    for (int i = 0; i < s.Length - 1; i++)
         
        // check if two consecutive
        // digits are same
        if (s[i] == s[i + 1])
            count++;
 
    return count;
}
 
// Driver code
public static void Main()
{
    int n = 44522255;
    Console.Write(countConsecutive(n));
}
}
 
// This code is contributed
// by Akanksha Rai(Abby_akku)


PHP


Javascript


输出:
4

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。