📜  Repdigit数字

📅  最后修改于: 2021-05-07 07:21:50             🧑  作者: Mango

Repdigit Number是一个数字N ,在以B为基数的表示中,所有数字均相等。
某些Repdigit号码是:

检查N是否为重复数字

给定数字N ,任务是检查N是否为基数B中Repdigit数字。如果N是基数B中的代表数字,则打印“是”,否则打印“否”
例子:

方法:

  • 一对一地找到基数B中N的所有数字。
  • 将每个数字与其前一个数字进行比较。
  • 如果任何数字不等于前一个数字,则返回false。
  • 否则返回true。

下面是上述方法的实现:

C++
// C++ implementation to check
// if a number is Repdigit
 
#include 
using namespace std;
 
// Function to check if a number
// is a Repdigit number
bool isRepdigit(int num, int b)
{
    // To store previous digit (Assigning
    // initial value which is less than any
    // digit)
    int prev = -1;
 
    // Traverse all digits from right to
    // left and check if any digit is
    // smaller than previous.
    while (num) {
        int digit = num % b;
        num /= b;
        if (prev != -1 && digit != prev)
            return false;
        prev = digit;
    }
 
    return true;
}
 
// Driver code
int main()
{
    int num = 2000, base = 7;
    isRepdigit(num, base) ? cout << "Yes"
                          : cout << "No";
    return 0;
}


Java
// Java implementation to check
// if a number is Repdigit
class GFG{
 
// Function to check if a number
// is a Repdigit number
static boolean isRepdigit(int num, int b)
{
    // To store previous digit (Assigning
    // initial value which is less than any
    // digit)
    int prev = -1;
 
    // Traverse all digits from right to
    // left and check if any digit is
    // smaller than previous.
    while (num != 0)
    {
        int digit = num % b;
        num /= b;
        if (prev != -1 && digit != prev)
            return false;
        prev = digit;
    }
    return true;
}
 
// Driver code
public static void main(String args[])
{
    int num = 2000, base1 = 7;
    if(isRepdigit(num, base1))
    {
        System.out.print("Yes");
    }
    else
    {
        System.out.print("No");
    }
}
}
 
// This code is contributed by Code_Mech


Python3
# Python3 implementation to check
# if a number is Repdigit
 
# Function to check if a number
# is a Repdigit number
def isRepdigit(num, b) :
     
    # To store previous digit (Assigning
    # initial value which is less than any
    # digit)
    prev = -1
 
    # Traverse all digits from right to
    # left and check if any digit is
    # smaller than previous.
    while (num) :
        digit = num % b
        num //= b
        if (prev != -1 and digit != prev) :
            return False
        prev = digit
    return True
 
# Driver code
num = 2000
base = 7
if(isRepdigit(num, base)):
    print("Yes")
else:
    print("No")
 
# This code is contributed by Vishal Maurya.


C#
// C# implementation to check
// if a number is Repdigit
using System;
class GFG{
 
// Function to check if a number
// is a Repdigit number
static bool isRepdigit(int num, int b)
{
    // To store previous digit (Assigning
    // initial value which is less than any
    // digit)
    int prev = -1;
 
    // Traverse all digits from right to
    // left and check if any digit is
    // smaller than previous.
    while (num != 0)
    {
        int digit = num % b;
        num /= b;
        if (prev != -1 && digit != prev)
            return false;
        prev = digit;
    }
    return true;
}
 
// Driver code
public static void Main()
{
    int num = 2000, base1 = 7;
    if(isRepdigit(num, base1))
    {
        Console.Write("Yes");
    }
    else
    {
        Console.Write("No");
    }
}
}
 
// This code is contributed by Code_Mech


Javascript


输出:
Yes

时间复杂度: O(n)
参考:http://www.numbersaplenty.com/set/repdigit/