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

📅  最后修改于: 2021-09-07 04:35:37             🧑  作者: Mango

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

方法一:

  1. 将给定的数字A 和 B分别转换为字符串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
// suffix of A or not
bool checkSuffix(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[n1 - i - 1]
            != s2[n2 - i - 1]) {
            return false;
        }
    }
 
    // Return true
    return true;
}
 
// Driver Code
int main()
{
    // Given numbers
    int A = 12345, B = 45;
 
    // Function Call
    bool result = checkSuffix(A, B);
 
    // If B is a suffix 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 suffix of A or not
public static boolean checkSuffix(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(n1 - i - 1) !=
           s2.charAt(n2 - i - 1))
       {
           return false;
       }
    }
     
    // Return true
    return true;
}
 
// Driver code
public static void main(String[] args)
{
         
    // Given numbers
    int A = 12345, B = 45;
     
    // Function Call
    boolean result = checkSuffix(A, B);
     
    // If B is a suffix of A, 
    // then print "Yes"
    if (result)
    {
        System.out.print("Yes");
    }
    else
    {
        System.out.println("No");
    }
}
}
 
// This code is contributed by divyeshrabadiya07


Python3
# Python3 program for the above approach
 
# Function to check if B is a
# suffix of A or not
def checkSuffix(A, B):
 
    # Convert numbers into strings
    s1 = str(A);
    s2 = str(B);
 
    # Find the lengths of strings
    # s1 and s2
    n1 = len(s1)
    n2 = len(s2)
 
    # Base Case
    if (n1 < n2):
        return False;
     
    # Traverse the strings s1 & s2
    for i in range(n2):
 
        # If at any index characters
        # are unequals then return false
        if (s1[n1 - i - 1] != s2[n2 - i - 1]):
            return False;
             
    # Return true
    return True;
     
# Driver Code
 
# Given numbers
A = 12345
B = 45;
 
# Function Call
result = checkSuffix(A, B);
 
# If B is a suffix of A, then
# print "Yes"
if (result):
    print("Yes")
else:
    print("No")
 
# This code is contributed by grand_master


C#
// C# program for the above approach
using System;
class GFG{
     
// Function to check if B
// is a suffix of A or not
public static bool checkSuffix(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[n1 - i - 1] !=  s2[n2 - i - 1])
        {
            return false;
        }
    }
     
    // Return true
    return true;
}
 
// Driver code
public static void Main(string[] args)
{
         
    // Given numbers
    int A = 12345, B = 45;
     
    // Function Call
    bool result = checkSuffix(A, B);
     
    // If B is a suffix of A,
    // then print "Yes"
    if (result)
    {
        Console.Write("Yes");
    }
    else
    {
        Console.Write("No");
    }
}
}
 
// This code is contributed by rutvik_56


Javascript


C++
// C++ program for the above approach
#include 
#include 
using namespace std;
 
// Function to check if B is a
// suffix of A or not
void checkSuffix(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 suffix of s1
    // or not using ends_with() function
    result = boost::algorithm::ends_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 = 45;
 
    // Function Call
    checkSuffix(A, B);
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to check if B is a
// suffix of A or not
static void checkSuffix(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 suffix of s1
    // or not
    result = s1.endsWith(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 = 45;
 
    // Function Call
    checkSuffix(A, B);
}
}
 
// This code is contributed by 29AjayKumar


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


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to check if B is a
// suffix of A or not
static void checkSuffix(int A, int B)
{
 
    // Convert numbers into Strings
    String s1 = String.Join("", A);
    String s2 = String.Join("", B);
 
    bool result;
 
    // Check if s2 is a suffix of s1
    // or not
    result = s1.EndsWith(s2);
 
    // If result is true, print "Yes"
    if (result)
    {
        Console.Write("Yes");
    }
    else
    {
        Console.Write("No");
    }
}
 
// Driver Code
public static void Main(String[] args)
{
    // Given numbers
    int A = 12345, B = 45;
 
    // Function Call
    checkSuffix(A, B);
}
}
 
// This code is contributed by 29AjayKumar


C++
// C++ program for the above approach
#include 
#include 
using namespace std;
 
// Function to check if B is a
// suffix of A or not
bool checkSuffix(int A, int B)
{
 
    // Find the number of digit in B
    int digit_B = log10(B) + 1;
 
    // Subtract B from A
    A -= B;
 
    // Returns true,
    // if B is a suffix of A
    return (A % int(pow(10, digit_B)));
}
 
// Driver Code
int main()
{
    // Given numbers
    int A = 12345, B = 45;
 
    // Function Call
    bool result = checkSuffix(A, B);
 
    // If B is a suffix of A, then
    // print "Yes"
    if (!result) {
        cout << "Yes";
    }
    else {
        cout << "No";
    }
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to check if B
// is a suffix of A or not
static boolean checkSuffix(int A, int B)
{
 
    // Find the number of digit in B
    int digit_B = (int) (Math.log10(B) + 1);
 
    // Subtract B from A
    A -= B;
 
    // Returns true,
    // if B is a suffix of A
    return (A % (int)(Math.pow(10, digit_B)) > 0);
}
 
// Driver code
public static void main(String[] args)
{
 
    // Given numbers
    int A = 12345, B = 45;
 
    // Function call
    boolean result = checkSuffix(A, B);
 
    // If B is a suffix of A,
    // then print "Yes"
    if (!result)
    {
        System.out.print("Yes");
    }
    else
    {
        System.out.print("No");
    }
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program for the above approach
import math
 
# Function to check if B is a
# suffix of A or not
def checkSuffix(A, B):
 
    # Find the number of digit in B
    digit_B = int(math.log10(B)) + 1;
 
    # Subtract B from A
    A -= B;
 
    # Returns true,
    # if B is a suffix of A
    return (A % int(math.pow(10, digit_B)));
 
# Driver Code
 
# Given numbers
A = 12345; B = 45;
 
# Function Call
result = checkSuffix(A, B);
 
# If B is a suffix of A, then
# print "Yes"
if (result == 0):
    print("Yes");
 
else:
    print("No");
 
# This code is contributed by Nidhi_biet


C#
// C# program for the above approach
using System;
class GFG{
 
// Function to check if B
// is a suffix of A or not
static bool checkSuffix(int A, int B)
{
 
    // Find the number of digit in B
    int digit_B = (int)(Math.Log10(B) + 1);
 
    // Subtract B from A
    A -= B;
 
    // Returns true,
    // if B is a suffix of A
    return (A % (int)(Math.Pow(10, digit_B)) > 0);
}
 
// Driver code
public static void Main()
{
 
    // Given numbers
    int A = 12345, B = 45;
 
    // Function call
    bool result = checkSuffix(A, B);
 
    // If B is a suffix of A,
    // then print "Yes"
    if (!result)
    {
        Console.Write("Yes");
    }
    else
    {
        Console.Write("No");
    }
}
}
 
// This code is contributed by Code_Mech


输出:
Yes

方法 2:使用内置函数std::boost::algorithm::ends_with()包含在 C++ 的 Boost 库中,用于检查任何字符串是否包含另一个字符串的后缀。
下面是上述方法的实现:

C++

// C++ program for the above approach
#include 
#include 
using namespace std;
 
// Function to check if B is a
// suffix of A or not
void checkSuffix(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 suffix of s1
    // or not using ends_with() function
    result = boost::algorithm::ends_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 = 45;
 
    // Function Call
    checkSuffix(A, B);
    return 0;
}

Java

// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to check if B is a
// suffix of A or not
static void checkSuffix(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 suffix of s1
    // or not
    result = s1.endsWith(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 = 45;
 
    // Function Call
    checkSuffix(A, B);
}
}
 
// This code is contributed by 29AjayKumar

蟒蛇3

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

C#

// C# program for the above approach
using System;
 
class GFG{
 
// Function to check if B is a
// suffix of A or not
static void checkSuffix(int A, int B)
{
 
    // Convert numbers into Strings
    String s1 = String.Join("", A);
    String s2 = String.Join("", B);
 
    bool result;
 
    // Check if s2 is a suffix of s1
    // or not
    result = s1.EndsWith(s2);
 
    // If result is true, print "Yes"
    if (result)
    {
        Console.Write("Yes");
    }
    else
    {
        Console.Write("No");
    }
}
 
// Driver Code
public static void Main(String[] args)
{
    // Given numbers
    int A = 12345, B = 45;
 
    // Function Call
    checkSuffix(A, B);
}
}
 
// This code is contributed by 29AjayKumar
输出:
Yes

方法三:

  1. A 中减去B。
  2. 使用本文中讨论的方法 3找出 B 中的位数(比如X )。
  3. 检查A是否以至少X个零结尾。如果是,则打印“是”
  4. 否则打印“否”

下面是上述方法的实现:

C++

// C++ program for the above approach
#include 
#include 
using namespace std;
 
// Function to check if B is a
// suffix of A or not
bool checkSuffix(int A, int B)
{
 
    // Find the number of digit in B
    int digit_B = log10(B) + 1;
 
    // Subtract B from A
    A -= B;
 
    // Returns true,
    // if B is a suffix of A
    return (A % int(pow(10, digit_B)));
}
 
// Driver Code
int main()
{
    // Given numbers
    int A = 12345, B = 45;
 
    // Function Call
    bool result = checkSuffix(A, B);
 
    // If B is a suffix of A, then
    // print "Yes"
    if (!result) {
        cout << "Yes";
    }
    else {
        cout << "No";
    }
    return 0;
}

Java

// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to check if B
// is a suffix of A or not
static boolean checkSuffix(int A, int B)
{
 
    // Find the number of digit in B
    int digit_B = (int) (Math.log10(B) + 1);
 
    // Subtract B from A
    A -= B;
 
    // Returns true,
    // if B is a suffix of A
    return (A % (int)(Math.pow(10, digit_B)) > 0);
}
 
// Driver code
public static void main(String[] args)
{
 
    // Given numbers
    int A = 12345, B = 45;
 
    // Function call
    boolean result = checkSuffix(A, B);
 
    // If B is a suffix of A,
    // then print "Yes"
    if (!result)
    {
        System.out.print("Yes");
    }
    else
    {
        System.out.print("No");
    }
}
}
 
// This code is contributed by 29AjayKumar

蟒蛇3

# Python3 program for the above approach
import math
 
# Function to check if B is a
# suffix of A or not
def checkSuffix(A, B):
 
    # Find the number of digit in B
    digit_B = int(math.log10(B)) + 1;
 
    # Subtract B from A
    A -= B;
 
    # Returns true,
    # if B is a suffix of A
    return (A % int(math.pow(10, digit_B)));
 
# Driver Code
 
# Given numbers
A = 12345; B = 45;
 
# Function Call
result = checkSuffix(A, B);
 
# If B is a suffix of A, then
# print "Yes"
if (result == 0):
    print("Yes");
 
else:
    print("No");
 
# This code is contributed by Nidhi_biet

C#

// C# program for the above approach
using System;
class GFG{
 
// Function to check if B
// is a suffix of A or not
static bool checkSuffix(int A, int B)
{
 
    // Find the number of digit in B
    int digit_B = (int)(Math.Log10(B) + 1);
 
    // Subtract B from A
    A -= B;
 
    // Returns true,
    // if B is a suffix of A
    return (A % (int)(Math.Pow(10, digit_B)) > 0);
}
 
// Driver code
public static void Main()
{
 
    // Given numbers
    int A = 12345, B = 45;
 
    // Function call
    bool result = checkSuffix(A, B);
 
    // If B is a suffix of A,
    // then print "Yes"
    if (!result)
    {
        Console.Write("Yes");
    }
    else
    {
        Console.Write("No");
    }
}
}
 
// This code is contributed by Code_Mech
输出:
Yes

时间复杂度: O(1)