📜  检查编号是否为回文数,是否在基数B中

📅  最后修改于: 2021-04-29 18:00:27             🧑  作者: Mango

给定整数N ,任务是检查是否

N_B

(碱基B中的N )是否为回文。

例子:

处理方法:可以通过检查是否反转的十进制值来解决该问题。

N_B

是否等于N。请按照以下步骤解决问题。

  1. 初始化变量rev = 0以存储N的倒数
  2. 提取的数字

N_B

  1. N%B
  2. 对于的每个数字

N_B

  1. 更新rev = rev * B + N%B
  2. 最后,检查N是否等于rev

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to check if N in
// base B is palindrome or not
int checkPalindromeB(int N, int B)
{
    // Stores the reverse of N
    int rev = 0;
 
    // Stores the value of N
    int N1 = N;
 
    // Extract all the digits of N
    while (N1) {
        // Generate its reverse
        rev = rev * B + N1 % B;
        N1 = N1 / B;
    }
 
    return N == rev;
}
 
// Driver Code
int main()
{
    int N = 5, B = 2;
    if (checkPalindromeB(N, B)) {
        cout << "Yes";
    }
    else {
        cout << "No";
    }
}


Java
// Java program to implement
// the above approach
class GFG{
 
// Function to check if N in
// base B is palindrome or not
static boolean checkPalindromeB(int N,
                                int B)
{
     
    // Stores the reverse of N
    int rev = 0;
 
    // Stores the value of N
    int N1 = N;
 
    // Extract all the digits of N
    while (N1 > 0)
    {
         
        // Generate its reverse
        rev = rev * B + N1 % B;
        N1 = N1 / B;
    }
    return N == rev;
}
 
// Driver code
public static void main(String[] args)
{
    int N = 5, B = 2;
     
    if (checkPalindromeB(N, B))
    {
        System.out.print("Yes");
    }
    else
    {
        System.out.print("No");
    }
}
}
 
// This code is contributed by Dewanti


Python3
# Python3 program to implement
# the above approach
 
# Function to check if N in
# base B is palindrome or not
def checkPalindromeB(N, B):
 
    # Stores the reverse of N
    rev = 0;
 
    # Stores the value of N
    N1 = N;
 
    # Extract all the digits of N
    while (N1 > 0):
 
        # Generate its reverse
        rev = rev * B + N1 % B;
        N1 = N1 // B;
     
    return N == rev;
 
# Driver code
if __name__ == '__main__':
    N = 5; B = 2;
 
    if (checkPalindromeB(N, B)):
        print("Yes");
    else:
        print("No");
 
# This code is contributed by Princi Singh


C#
// C# program to implement
// the above approach
using System;
 
class GFG{
 
// Function to check if N in
// base B is palindrome or not
static bool checkPalindromeB(int N,
                             int B)
{
     
    // Stores the reverse of N
    int rev = 0;
 
    // Stores the value of N
    int N1 = N;
 
    // Extract all the digits of N
    while (N1 > 0)
    {
         
        // Generate its reverse
        rev = rev * B + N1 % B;
        N1 = N1 / B;
    }
    return N == rev;
}
 
// Driver code
public static void Main(String[] args)
{
    int N = 5, B = 2;
     
    if (checkPalindromeB(N, B))
    {
        Console.Write("Yes");
    }
    else
    {
        Console.Write("No");
    }
}
}
 
// This code is contributed by Amit Katiyar


Javascript


输出:
Yes

时间复杂度: O(log B N)
辅助空间: O(1)