📜  检查数字是否仅由1、14或144串联而成

📅  最后修改于: 2021-04-29 15:59:09             🧑  作者: Mango

给定一个数字N 。任务是检查数字是否仅通过将数字1、14和144任意次数和以任何顺序连接而形成。

如果可能,请打印“是”,否则打印“否”。

例子:

Input: N = 141411
Output: YES 

Input: N = 14134
Output: NO

想法是从末尾获取一位数字,两位数字和三位数字,并检查它们是否分别与1、14和144匹配。如果它们中的任何一个匹配,请用该数字除以该数字,然后重复上述步骤,直到该数字大于零为止。

下面是使用上述方法的实现:

C++
// C++ program to check if a number is formed
// by Concatenation of 1, 14 or 144 only
  
#include 
using namespace std;
  
// Function to check if a number is formed
// by Concatenation of 1, 14 or 144 only
string checkNumber(int N)
{
    int temp = N;
  
    while (temp > 0) {
        // check for each possible digit
        // if given number consist other then
        // 1, 14, 144 print NO else print YES
        if (temp % 1000 == 144)
            temp /= 1000;
        else if (temp % 100 == 14)
            temp /= 100;
        else if (temp % 10 == 1)
            temp /= 10;
        else {
            return "NO";
        }
    }
  
    return "YES";
}
  
// Driver Code
int main()
{
    int N = 1414;
  
    cout << checkNumber(N);
  
    return 0;
}


Java
// Java program to check if a number is formed
// by Concatenation of 1, 14 or 144 only
  
import java.io.*;
  
class GFG {
      
  
// Function to check if a number is formed
// by Concatenation of 1, 14 or 144 only
static String checkNumber(int N)
{
    int temp = N;
  
    while (temp > 0) {
        // check for each possible digit
        // if given number consist other then
        // 1, 14, 144 print NO else print YES
        if (temp % 1000 == 144)
            temp /= 1000;
        else if (temp % 100 == 14)
            temp /= 100;
        else if (temp % 10 == 1)
            temp /= 10;
        else {
            return "NO";
        }
    }
  
    return "YES";
}
  
// Driver Code
  
  
    public static void main (String[] args) {
        int N = 1414;
  
    System.out.println(checkNumber(N));
    }
}
// This code is contributed by anuj_67..


Python 3
# Python 3 program to check if a 
# number is formed by Concatenation 
# of 1, 14 or 144 only
  
# Function to check if a number is formed
# by Concatenation of 1, 14 or 144 only
def checkNumber(N):
    temp = N
  
    while (temp > 0):
          
        # check for each possible digit
        # if given number consist other then
        # 1, 14, 144 print NO else print YES
        if (temp % 1000 == 144):
            temp /= 1000
        elif (temp % 100 == 14):
            temp /= 100
        elif (temp % 10 == 1):
            temp /= 10
        else: 
            return "YES"
  
    return "NO"
  
# Driver Code
N = 1414;
  
print(checkNumber(N));
  
# This code is contributed 
# by Akanksha Rai


C#
// C# program to check if a number is formed
// by Concatenation of 1, 14 or 144 only
  
using System;
  
class GFG {
      
  
// Function to check if a number is formed
// by Concatenation of 1, 14 or 144 only
static String checkNumber(int N)
{
    int temp = N;
  
    while (temp > 0) {
        // check for each possible digit
        // if given number consist other then
        // 1, 14, 144 print NO else print YES
        if (temp % 1000 == 144)
            temp /= 1000;
        else if (temp % 100 == 14)
            temp /= 100;
        else if (temp % 10 == 1)
            temp /= 10;
        else {
            return "NO";
        }
    }
  
    return "YES";
}
  
// Driver Code
  
  
    public static void Main () {
        int N = 1414;
  
    Console.WriteLine(checkNumber(N));
    }
}
// This code is contributed by anuj_67..


PHP
 0) 
    {
        // check for each possible digit
        // if given number consist other then
        // 1, 14, 144 print NO else print YES
        if ($temp % 1000 == 144)
            $temp /= 1000;
        else if ($temp % 100 == 14)
            $temp /= 100;
        else if ($temp % 10 == 1)
            $temp /= 10;
        else 
        {
            return "YES";
        }
    }
  
    return "NO";
}
  
// Driver Code
$N = 1414;
echo checkNumber($N);
  
// This code is contributed by Tushil
?>


输出:
YES