📜  整理编号(数字降序排列)

📅  最后修改于: 2021-04-26 07:19:32             🧑  作者: Mango

给定一个数字,任务是检查它是否整洁。整齐的数字是数字以非降序排列的数字。
例子 :

Input : 1234
Output : Yes

Input : 1243
Output : No
Digits "4" and "3" violate the property.

在Freshokartz中问

算法:

1- One by one find all the digits.
2- Compare every digit with its next digit.
3- If any is in decreasing order then return false.
4- Otherwise return true.

执行 :

C++
// C++ program to check if a number is Tidy
// or not.
#include
using namespace std;
 
// Returns true if num is Tidy
bool isTidy(int num)
{
    // To store previous digit (Assigning
    // initial value which is more than any
    // digit)
    int prev = 10;
 
    // Traverse all digits from right to
    // left and check if any digit is
    // smaller than previous.
    while (num)
    {
        int rem = num % 10;
        num /= 10;
        if (rem > prev)
           return false;
        prev = rem;
    }
 
    return true;
}
 
// Driver code
int main()
{
    int num = 1556;
    isTidy(num) ? cout << "Yes"
                : cout << "No";
    return 0;
}


Java
// Java program to check if a number
// is Tidy or not.
 
class Test
{
    // Returns true if num is Tidy
    static boolean isTidy(int num)
    {
        // To store previous digit
        // (Assigning initial value
            // which is more than any
        // digit)
        int prev = 10;
      
        // Traverse all digits from right to
        // left and check if any digit is
        // smaller than previous.
        while (num!=0)
        {
            int rem = num % 10;
            num /= 10;
            if (rem > prev)
               return false;
            prev = rem;
        }
      
        return true;
    }
     
    // Driver method
    public static void main(String[] args)
    {
        int num = 1556;
        System.out.println(isTidy(num) ? "Yes" : "No");
    }
}


Python3
# Python program to check if a number
# is Tidy or not.
 
# Returns true if num is Tidy
def isTidy(num):
 
    # To store previous digit (Assigning
    # initial value which is more than any
    # digit)
    prev = 10
     
    # Traverse all digits from right to
    # left and check if any digit is
    # smaller than previous.
    while (num):
        rem = num % 10
        num /= 10
        if rem > prev:
            return False
        prev = rem
    return True
 
# Driver code
num = 1556
if isTidy(num):
    print("Yes")
else:
    print("No")
 
# This code is contributed by Sharad_Bhardwaj.


C#
// C# program to check if a
// number is Tidy or not.
using System;
 
class GFG
{
    // Returns true if num is Tidy
    static bool isTidy(int num)
    {
        // To store previous digit
        // (Assigning initial value
        // which is more than any
        // digit)
        int prev = 10;
     
        // Traverse all digits from
        // right to left and check
        // if any digit is smaller
        // than previous.
        while (num != 0)
        {
            int rem = num % 10;
            num /= 10;
            if (rem > prev)
            return false;
            prev = rem;
        }
     
        return true;
    }
     
// Driver Code
public static void Main ()
{
    int num = 1556;
 
    Console.WriteLine(isTidy(num) ?
                            "Yes" :
                             "No");
}
}
 
// This code is contributed by m_kit


PHP
 $prev)
            return false;
        $prev = $rem;
    }
 
    return true;
}
 
// Driver code
$num = 1556;
if(isTidy($num) == true)
echo "Yes";
else
echo "No";
 
// This code is contributed by aj_36
?>


Javascript


输出 :

Yes