📌  相关文章
📜  检查数字在给定的基数中是否具有连续的0

📅  最后修改于: 2021-04-29 03:38:40             🧑  作者: Mango

给定十进制数字N,任务是在将数字转换为其基于K的表示法之后检查数字是否具有连续的零。

例子:

方法:首先将数字N转换为基数K,然后简单地检查数字是否具有连续的零。

下面是上述方法的实现:

C++
// C++ implementation of the above approach
#include
using namespace std;
 
 
 
// Function to convert N into base K
int toK(int N, int K)
{
 
// Weight of each digit
    int w = 1;
    int s = 0;
    while (N != 0)
     {
        int r = N % K;
        N = N/K;
        s = r * w + s;
        w *= 10;
     }
    return s;
 
}
 
// Function to check for consecutive 0
bool check(int N)
{
 
// Flag to check if there are consecutive
    // zero or not
    bool fl = false;
    while (N != 0)
    {
 
        int r = N % 10;
        N = N/10;
 
        // If there are two consecutive zero
        // then returning False
        if (fl == true and r == 0)
            return false;
        if (r > 0)
            {
            fl = false;
            continue;
            }
        fl = true;
 
    }
     return true;
         
}
 
// We first convert to given base, then
// check if the converted number has two
// consecutive 0s or not
void hasConsecutiveZeroes(int N, int K)
{
    int z = toK(N, K);
    if (check(z))
       cout<<"Yes"<


Java
// Java implementation of the above approach
import java.util.*;
 
class GFG
{
 
// Function to convert N into base K
static int toK(int N, int K)
{
 
    // Weight of each digit
    int w = 1;
    int s = 0;
    while (N != 0)
    {
        int r = N % K;
        N = N / K;
        s = r * w + s;
        w *= 10;
    }
    return s;
 
}
 
// Function to check for consecutive 0
static boolean check(int N)
{
 
    // Flag to check if there are consecutive
    // zero or not
    boolean fl = false;
    while (N != 0)
    {
 
        int r = N % 10;
        N = N / 10;
 
        // If there are two consecutive zero
        // then returning False
        if (fl == true && r == 0)
            return false;
        if (r > 0)
        {
            fl = false;
            continue;
        }
        fl = true;
    }
    return true;
}
 
// We first convert to given base, then
// check if the converted number has two
// consecutive 0s or not
static void hasConsecutiveZeroes(int N, int K)
{
    int z = toK(N, K);
    if (check(z))
        System.out.println("Yes");
    else
        System.out.println("No");
}
 
// Driver code
public static void main(String[] args)
{
    int N = 15;
    int K = 8;
    hasConsecutiveZeroes(N, K);
}
}
 
// This code is contributed by Princi Singh


Python3
# Python implementation of the above approach
 
# We first convert to given base, then
# check if the converted number has two
# consecutive 0s or not
def hasConsecutiveZeroes(N, K):
    z = toK(N, K)
    if (check(z)):
        print("Yes")
    else:
        print("No")
 
# Function to convert N into base K
def toK(N, K):
 
    # Weight of each digit
    w = 1
    s = 0
    while (N != 0):
        r = N % K
        N = N//K
        s = r * w + s
        w* = 10
    return s
 
# Function to check for consecutive 0
def check(N):
 
    # Flag to check if there are consecutive
    # zero or not
    fl = False
    while (N != 0):
        r = N % 10
        N = N//10
 
        # If there are two consecutive zero
        # then returning False
        if (fl == True and r == 0):
            return False
        if (r > 0):
            fl = False
            continue
        fl = True
    return True
 
# Driver code
N, K = 15, 8
hasConsecutiveZeroes(N, K)


C#
// C# implementation of the above approach
using System;
 
class GFG
{
 
// Function to convert N into base K
static int toK(int N, int K)
{
 
    // Weight of each digit
    int w = 1;
    int s = 0;
    while (N != 0)
    {
        int r = N % K;
        N = N / K;
        s = r * w + s;
        w *= 10;
    }
    return s;
}
 
// Function to check for consecutive 0
static Boolean check(int N)
{
 
    // Flag to check if there are consecutive
    // zero or not
    Boolean fl = false;
    while (N != 0)
    {
 
        int r = N % 10;
        N = N / 10;
 
        // If there are two consecutive zero
        // then returning False
        if (fl == true && r == 0)
            return false;
        if (r > 0)
        {
            fl = false;
            continue;
        }
        fl = true;
    }
    return true;
}
 
// We first convert to given base, then
// check if the converted number has two
// consecutive 0s or not
static void hasConsecutiveZeroes(int N, int K)
{
    int z = toK(N, K);
    if (check(z))
        Console.WriteLine("Yes");
    else
        Console.WriteLine("No");
}
 
// Driver code
public static void Main(String[] args)
{
    int N = 15;
    int K = 8;
    hasConsecutiveZeroes(N, K);
}
}
 
// This code is contributed by 29AjayKumar


PHP
 0)
        {
            $fl = false;
            continue;
        }
        $fl = true;
    }
    return true;
}
 
// Driver code
$N = 15;
$K = 8;
hasConsecutiveZeroes($N, $K);
 
// This code is contributed by mits
?>


Javascript


输出:
Yes