📜  检查数字是否混乱

📅  最后修改于: 2021-04-27 22:05:17             🧑  作者: Mango

编写程序以检查给定的整数是否杂乱。如果每个数字的邻居数字相差最大为1,则该数字被认为是混杂的。

例子 :

Input : 6765
Output : True
All neighbour digits differ by atmost 1.

Input : 1223
Output : True

Input : 1235
Output : False

要查找数字是否混乱,我们只需要检查一个数字是否有邻居
差异大于1。如果找到这样的数字,则该数字为非乱码。

下面是上述想法的实现:

C++
// CPP code to check if a 
// number is jumbled or not
#include 
using namespace std;
  
// Function to check if a 
// number is jumbled or not
bool checkJumbled(int num)
{
    // Single digit number
    if (num / 10 == 0)
        return true;
  
    // Checking every digit 
    // through a loop
    while (num != 0) 
    {
  
        // All digits were checked
        if (num / 10 == 0)
            return true;
  
        // Digit at index i
        int digit1 = num % 10;
  
        // Digit at index i-1
        int digit2 = (num / 10) % 10;
  
        // If difference is 
        // greater than 1
        if (abs(digit2 - digit1) > 1)
            return false;
  
        num = num / 10;
    }
  
    // Number checked
    return true;
}
  
// Driver code
int main()
{
  
    //-1234 to be checked
    int num = -1234;
  
    if (checkJumbled(num))
        cout << "True \n";
    else
        cout << "False \n";
  
    // 287 to be checked
    num = -1247;
  
    if (checkJumbled(num))
        cout << "True \n";
    else
        cout << "False \n";
  
    return 0;
}


Java
// Java code to check if a
// number is jumbled or not
import java.io.*;
  
class GFG
{
      
    // Function to check if a
    // number is jumbled or not
    static boolean checkJumbled(int num)
    {
        // Single digit number
        if (num / 10 == 0)
            return true;
      
        // Checking every digit
        // through a loop
        while (num != 0) 
        {
      
            // All digits were checked
            if (num / 10 == 0)
                return true;
      
            // Digit at index i
            int digit1 = num % 10;
      
            // Digit at index i-1
            int digit2 = (num / 10) % 10;
      
            // If difference is 
            // greater than 1
            if (Math.abs(digit2 - digit1) > 1)
                return false;
      
            num = num / 10;
        }
      
        // Number checked
        return true;
    }
      
    // Driver code
    public static void main (String[]args)
    {
        //-1234 to be checked
        int num = -1234;
      
        if (checkJumbled(num))
            System.out.println("True ");
        else
            System.out.println("False ");
      
        // 287 to be checked
        num = -1247;
      
        if (checkJumbled(num))
            System.out.println("True ");
                  
        else
            System.out.println("False ");
              
    }
}
  
// This code is contributed by vt_m.


Python
# Python code to check if 
# a number is jumbled or not
  
# Function to check if a
# number is jumbled or not
def checkJumbled(num):
  
    # Single digit number
    if (num / 10 == 0):
        return True
  
    # Checking every digit
    # through a loop
    while (num != 0):
          
        # All digits were checked
        if (num / 10 == 0):
            return True
  
        # Digit at index i
        digit1 = num % 10
  
        # Digit at index i-1
        digit2 = (num / 10) % 10
  
        # If difference is 
        # greater than 1
        if (abs(digit2 - digit1) > 1):
            return False
  
        num = num / 10
          
    # Number checked
    return True
      
# Driver code
  
# -1234 to be checked
num = -1234
if (checkJumbled(abs(num))):
    print "True "
else:
    print "False"
      
# -1247 to be checked
num = -1247
if (checkJumbled(abs(num))):
    print "True "
else:
    print "False"
  
# This code is contributed 
# by Sachin Bisht


C#
// C# code to check if a number 
// is jumbled or not
using System;
  
class GFG 
{
      
    // Function to check if a 
    // number is jumbled or not
    static bool checkJumbled(int num)
    {
          
        // Single digit number
        if (num / 10 == 0)
            return true;
      
        // Checking every digit 
        // through a loop
        while (num != 0) 
        {
      
            // All digits were checked
            if (num / 10 == 0)
                return true;
      
            // Digit at index i
            int digit1 = num % 10;
      
            // Digit at index i-1
            int digit2 = (num / 10) % 10;
      
            // If difference is 
            // greater than 1
            if (Math.Abs(digit2 - digit1) > 1)
                return false;
      
            num = num / 10;
        }
      
        // Number checked
        return true;
    }
      
    // Driver code
    public static void Main ()
    {
        //-1234 to be checked
        int num = -1234;
      
        if (checkJumbled(num))
            Console.WriteLine("True ");
        else
            Console.WriteLine("False ");
      
        // 287 to be checked
        num = -1247;
      
        if (checkJumbled(num))
            Console.WriteLine("True");
                  
        else
            Console.WriteLine("False");
              
    }
}
  
// This code is contributed by Nitin Mittal.


PHP
 1)
            return false;
  
        $num = $num / 10;
    }
  
    // Number checked
    return true;
}
  
// Driver code
  
//-1234 to be checked
$num = -1234;
  
if (checkJumbled($num))
    echo "True \n";
else
    echo "False \n";
  
// 287 to be checked
$num = -1247;
  
if (checkJumbled($num))
    echo "True \n";
else
    echo "False \n";
  
// This code is contributed by ajit
?>


输出 :

True
False

相关文章:
步进数