📜  卡塔德罗姆编号

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

Katadrome数字是数字以降序排列的数字。
很少的Katadrome数字是:

检查N是否是Katadromes

给定数字N ,任务是检查它是否是Katadromes。
例子:

方法:想法是遍历数字的位数,并检查当前数字是否小于最后一位。如果所有数字均满足条件,则该数字为Katadrome数字。
下面是上述方法的实现:

C++
// C++ implementation to check if
// a number is Katadrome or not.
 
#include 
using namespace std;
 
// Function to check if a number
// is a Katadrome number or not
bool isKatadrome(int num)
{
    // 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 % 10;
        num /= 10;
        if (digit < prev)
            return false;
        prev = digit;
    }
 
    return true;
}
 
// Driver code
int main()
{
    int num = 4321;
    isKatadrome(num) ? cout << "Yes"
                     : cout << "No";
    return 0;
}


Java
// Java implementation to check if
// a number is Katadrome or not.
class GFG{
 
// Function to check if a number
// is a Katadrome number or not
static boolean isKatadrome(int num)
{
     
    // 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 % 10;
        num /= 10;
        if (digit < prev)
            return false;
        prev = digit;
    }
    return true;
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 4321;
 
    // Function Call
    if (isKatadrome(N))
        System.out.print("Yes");
    else
        System.out.print("No");
}
}
 
// This code is contributed by Pratima Pandey


Python3
# Python3 program to print count of values such
# that n+i = n^i
 
def isKatadrome(num):
     
    # 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 % 10
        num //= 10
        if digit < prev:
            return False
        prev = digit
         
    return True
 
# Driver code
if __name__=='__main__':
     
    num = 4321
     
    if isKatadrome(num):
        print('Yes')
    else:
        print('No')
 
# This code is contributed by rutvik


C#
// C# implementation to check if
// a number is Katadrome or not.
using System;
class GFG{
 
// Function to check if a number
// is a Katadrome number or not
static bool isKatadrome(int num)
{
     
    // 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 % 10;
        num /= 10;
        if (digit < prev)
            return false;
        prev = digit;
    }
    return true;
}
 
// Driver Code
public static void Main()
{
    int N = 4321;
 
    // Function Call
    if (isKatadrome(N))
        Console.Write("Yes");
    else
        Console.Write("No");
}
}
 
// This code is contributed by Code_Mech


Javascript


输出:
Yes

时间复杂度: O(d) ,其中d是给定数字中的位数。
参考: http : //www.numbersaplenty.com/set/katadrome/