给定一个数字,任务是检查它是否整洁。整齐的数字是数字按非降序排列的数字。 例如:
null
Input : 1234Output : YesInput : 1243Output : NoDigits "4" and "3" violate the property.
弗雷什奥卡茨问道
算法:
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<iostream> 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
<?php // PHP program to check if a // number is Tidy or not. // Returns true if num is Tidy function 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 = (int) $num / 10; if ( $rem > $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
<script> // JavaScript program for the above approach // Returns true if num is Tidy function isTidy(num) { // To store previous digit // (Assigning initial value // which is more than any // digit) let prev = 10; // Traverse all digits from right to // left and check if any digit is // smaller than previous. while (num!=0) { let rem = num % 10; num /= 10; if (rem > prev) return false ; prev = rem; } return true ; } // Driver Code let num = 1556; document.write(isTidy(num) ? "Yes" : "No" ); // This code is contributed by susmitakundugoaldanga. </script> |
输出:
Yes
时间复杂性: O(d),其中d是给定数字中的位数。 参考: https://www.careercup.com/question?id=5136136486780928 本文由 萨希尔·查布拉(阿克库) .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END