📌  相关文章
📜  检查一个数字是否以另一个数字开头

📅  最后修改于: 2021-09-06 05:06:10             🧑  作者: Mango

给定两个数字AB ,其中 ( A > B ),任务是检查 B 是否是 A 的前缀。如果是前缀则打印“是”否则打印“否”

例子:

方法:

  1. 将给定的数字AB分别转换为字符串str1str2
  2. 从字符串的开头遍历两个字符串。
  3. 在遍历字符串,如果str1str2中的任何索引字符不相等,则打印“No”
  4. 否则打印“是”。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include "bits/stdc++.h"
using namespace std;
 
// Function to check if B is a
// prefix of A or not
bool checkprefix(int A, int B)
{
 
    // Convert numbers into strings
    string s1 = to_string(A);
    string s2 = to_string(B);
 
    // Find the lengths of strings
    // s1 and s2
    int n1 = s1.length();
    int n2 = s2.length();
 
    // Base Case
    if (n1 < n2) {
        return false;
    }
 
    // Traverse the strings s1 & s2
    for (int i = 0; i < n2; i++) {
 
        // If at any index characters
        // are unequals then return false
        if (s1[i]
            != s2[i]) {
            return false;
        }
    }
 
    // Return true
    return true;
}
 
// Driver Code
int main()
{
    // Given numbers
    int A = 12345, B = 12;
 
    // Function Call
    bool result = checkprefix(A, B);
 
    // If B is a prefix of A, then
    // print "Yes"
    if (result) {
        cout << "Yes";
    }
    else {
        cout << "No";
    }
    return 0;
}


Java
// Java program for the above approach
class GFG{
 
// Function to check if B is a
// prefix of A or not
static boolean checkprefix(int A, int B)
{
     
    // Convert numbers into Strings
    String s1 = String.valueOf(A);
    String s2 = String.valueOf(B);
 
    // Find the lengths of Strings
    // s1 and s2
    int n1 = s1.length();
    int n2 = s2.length();
 
    // Base Case
    if (n1 < n2)
    {
        return false;
    }
 
    // Traverse the Strings s1 & s2
    for(int i = 0; i < n2; i++)
    {
         
        // If at any index characters
        // are unequals then return false
        if (s1.charAt(i) != s2.charAt(1))
        {
            return false;
        }
    }
 
    // Return true
    return true;
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given numbers
    int A = 12345, B = 12;
 
    // Function call
    boolean result = checkprefix(A, B);
 
    // If B is a prefix of A, then
    // print "Yes"
    if (!result)
    {
        System.out.print("Yes");
    }
    else
    {
        System.out.print("No");
    }
}
}
 
// This code is contributed by amal kumar choubey


Python3
# Python3 program for the
# above approach
 
# Function to check if B is
# a prefix of A or not
def checkprefix(A, B):
     
    # Convert numbers into strings
    s1 = str(A)
    s2 = str(B)
     
    # Find the length of s1 and s2
    n1 = len(s1)
    n2 = len(s2)
     
    # Base case
    if n1 < n2:
        return False
         
    # Traverse the string s1 and s2
    for i in range(0, n2):
         
        # If at any index characters
        # are unequal then return False
        if s1[i] != s2[i]:
            return False
             
    return True
 
# Driver code
if __name__=='__main__':
     
    # Given numbers
    A = 12345
    B = 12
     
    # Fucntion call
    result = checkprefix(A, B)
     
    # If B is a prefix of A ,
    # then print Yes
    if result:
        print("Yes")
    else:
        print("No")
         
# This code is contributed by virusbuddah_


C#
// C# program for the above approach
using System;
 
class GFG{
     
// Function to check if B is a
// prefix of A or not
static bool checkprefix(int A, int B)
{
 
    // Convert numbers into Strings
    String s1 = A.ToString();
    String s2 = B.ToString();
 
    // Find the lengths of Strings
    // s1 and s2
    int n1 = s1.Length;
    int n2 = s2.Length;
 
    // Base Case
    if (n1 < n2)
    {
        return false;
    }
 
    // Traverse the Strings s1 & s2
    for(int i = 0; i < n2; i++)
    {
 
        // If at any index characters
        // are unequals then return false
        if (s1[i] != s2[i])
        {
            return false;
        }
    }
 
    // Return true
    return true;
}
 
// Driver Code
static public void Main ()
{
     
    // Given numbers
    int A = 12345, B = 12;
 
    // Function call
    bool result = checkprefix(A, B);
 
    // If B is a prefix of A, then
    // print "Yes"
    if (result)
    {
        Console.Write("Yes");
    }
    else
    {
        Console.Write("No");
    }
}
}
 
// This code is contributed by shubhamsingh10


Javascript


C++
// C++ program for the above approach
#include 
#include 
using namespace std;
 
// Function to check if B is a
// prefix of A or not
void checkprefix(int A, int B)
{
 
    // Convert numbers into strings
    string s1 = to_string(A);
    string s2 = to_string(B);
 
    bool result;
 
    // Check if s2 is a prefix of s1
    // or not using starts_with() function
    result = boost::algorithm::starts_with(s1, s2);
 
    // If result is true, print "Yes"
    if (result) {
        cout << "Yes";
    }
    else {
        cout << "No";
    }
}
 
// Driver Code
int main()
{
    // Given numbers
    int A = 12345, B = 12;
 
    // Function Call
    checkprefix(A, B);
    return 0;
}


Java
// Java program for the above approach
class GFG{
 
// Function to check if B is a
// prefix of A or not
static void checkprefix(int A, int B)
{
 
    // Convert numbers into Strings
    String s1 = String.valueOf(A);
    String s2 = String.valueOf(B);
 
    boolean result;
 
    // Check if s2 is a prefix of s1
    // or not using starts_with() function
    result = s1.startsWith(s2);
 
    // If result is true, print "Yes"
    if (result)
    {
        System.out.print("Yes");
    }
    else
    {
        System.out.print("No");
    }
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given numbers
    int A = 12345, B = 12;
 
    // Function call
    checkprefix(A, B);
}
}
 
// This code is contributed by amal kumar choubey


Python3
# Python3 program for the
# above approach
 
# Function to check if B is
# a prefix of A or not
def checkprefix(A, B):
     
    # Convert numbers into strings
    s1 = str(A)
    s2 = str(B)
     
    # Check if s2 is a prefix of s1
    # or not using startswith() function
    result = s1.startswith(s2)
     
    # If result is true print Yes
    if result:
        print("Yes")
    else:
        print("No")
 
# Driver code
if __name__=='__main__':
     
    # Given numbers
    A = 12345
    B = 12
     
    # Function call
    checkprefix(A, B)
     
# This code is contributed by virusbuddah_


C#
// C# program for the above approach
using System.Threading;
using System.Globalization;
using System;
 
class GFG{
     
// Function to check if B is a
// prefix of A or not
static void checkprefix(int A, int B)
{
 
    // Convert numbers into Strings
    string s1 = A.ToString();
    string s2 = B.ToString();
 
    bool result;
 
    // Check if s2 is a prefix of s1
    // or not using starts_with() function
    result = s1.StartsWith(s2, false,
        CultureInfo.InvariantCulture);
 
    // If result is true, print "Yes"
    if (result)
    {
        Console.Write("Yes");
    }
    else
    {
        Console.Write("No");
    }
}
 
// Driver code
static void Main()
{
     
    // Given numbers
    int A = 12345, B = 12;
     
    // Function call
    checkprefix(A, B);
}
}
 
// This code is contributed by divyeshrabadiya07


输出:
Yes

使用内置函数:使用内置函数std::boost::algorithm::starts_with() ,可以检查任何字符串是否包含另一个字符串的前缀。
下面是上述方法的实现:

C++

// C++ program for the above approach
#include 
#include 
using namespace std;
 
// Function to check if B is a
// prefix of A or not
void checkprefix(int A, int B)
{
 
    // Convert numbers into strings
    string s1 = to_string(A);
    string s2 = to_string(B);
 
    bool result;
 
    // Check if s2 is a prefix of s1
    // or not using starts_with() function
    result = boost::algorithm::starts_with(s1, s2);
 
    // If result is true, print "Yes"
    if (result) {
        cout << "Yes";
    }
    else {
        cout << "No";
    }
}
 
// Driver Code
int main()
{
    // Given numbers
    int A = 12345, B = 12;
 
    // Function Call
    checkprefix(A, B);
    return 0;
}

Java

// Java program for the above approach
class GFG{
 
// Function to check if B is a
// prefix of A or not
static void checkprefix(int A, int B)
{
 
    // Convert numbers into Strings
    String s1 = String.valueOf(A);
    String s2 = String.valueOf(B);
 
    boolean result;
 
    // Check if s2 is a prefix of s1
    // or not using starts_with() function
    result = s1.startsWith(s2);
 
    // If result is true, print "Yes"
    if (result)
    {
        System.out.print("Yes");
    }
    else
    {
        System.out.print("No");
    }
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given numbers
    int A = 12345, B = 12;
 
    // Function call
    checkprefix(A, B);
}
}
 
// This code is contributed by amal kumar choubey

蟒蛇3

# Python3 program for the
# above approach
 
# Function to check if B is
# a prefix of A or not
def checkprefix(A, B):
     
    # Convert numbers into strings
    s1 = str(A)
    s2 = str(B)
     
    # Check if s2 is a prefix of s1
    # or not using startswith() function
    result = s1.startswith(s2)
     
    # If result is true print Yes
    if result:
        print("Yes")
    else:
        print("No")
 
# Driver code
if __name__=='__main__':
     
    # Given numbers
    A = 12345
    B = 12
     
    # Function call
    checkprefix(A, B)
     
# This code is contributed by virusbuddah_

C#

// C# program for the above approach
using System.Threading;
using System.Globalization;
using System;
 
class GFG{
     
// Function to check if B is a
// prefix of A or not
static void checkprefix(int A, int B)
{
 
    // Convert numbers into Strings
    string s1 = A.ToString();
    string s2 = B.ToString();
 
    bool result;
 
    // Check if s2 is a prefix of s1
    // or not using starts_with() function
    result = s1.StartsWith(s2, false,
        CultureInfo.InvariantCulture);
 
    // If result is true, print "Yes"
    if (result)
    {
        Console.Write("Yes");
    }
    else
    {
        Console.Write("No");
    }
}
 
// Driver code
static void Main()
{
     
    // Given numbers
    int A = 12345, B = 12;
     
    // Function call
    checkprefix(A, B);
}
}
 
// This code is contributed by divyeshrabadiya07
输出:
Yes