📌  相关文章
📜  最后加上一个数字,使数字等于其余字符串的长度

📅  最后修改于: 2021-05-06 20:41:06             🧑  作者: Mango

给定一个字符串str ,该字符串的末尾附加一个整数(带或不带前导零)。任务是从范围[0,9]中找到一个数字,该数字必须附加在整数的末尾,以使数字等于剩余字符串的长度。如果无法打印,请打印-1

例子:

方法:找到附加在字符串末尾的数字,例如num,并在末尾附加一个0 ,该数字应为最小位数,即num = num * 10 。现在找到剩余字符串的长度,忽略结尾处的数字len 。现在必须添加的数字将为digit = len – num 。如果digit[0,9]范围内则打印它,否则打印-1

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the requried digit
int find_digit(string s, int n)
{
  
    // To store the position of the first
    // numeric digit in the string
    int first_digit = -1;
    for (int i = n - 1; i >= 0; i--) {
        if (s[i] < '0' || s[i] > '9') {
            first_digit = i;
            break;
        }
    }
    first_digit++;
  
    // To store the length of the
    // string without the numeric
    // digits in the end
    int s_len = first_digit;
  
    // pw stores the current power of 10
    // and num is to store the number
    // which is appended in the end
    int num = 0, pw = 1;
    int i = n - 1;
    while (i >= 0) {
  
        // If current character is
        // a numeric digit
        if (s[i] >= '0' && s[i] <= '9') {
  
            // Get the current digit
            int digit = s[i] - '0';
  
            // Build the number
            num = num + (pw * digit);
  
            // If number exceeds the length
            if (num >= s_len)
                return -1;
  
            // Next power of 10
            pw = pw * 10;
        }
        i--;
    }
  
    // Append 0 in the end
    num = num * 10;
  
    // Required number that must be added
    int req = s_len - num;
  
    // If number is not a single digit
    if (req > 9 || req < 0)
        return -1;
    return req;
}
  
// Driver code
int main()
{
    string s = "abcd0";
    int n = s.length();
  
    cout << find_digit(s, n);
  
    return 0;
}


Java
// Java implementation of the approach
import java.io.*;
  
class GFG 
{
  
// Function to return the requried digit
static int find_digit(String s, int n)
{
  
    // To store the position of the first
    // numeric digit in the string
    int first_digit = -1;
    for (int i = n - 1; i >= 0; i--)
    {
        if (s.charAt(i) < '0' || 
            s.charAt(i) > '9') 
        {
            first_digit = i;
            break;
        }
    }
    first_digit++;
  
    // To store the length of the
    // string without the numeric
    // digits in the end
    int s_len = first_digit;
  
    // pw stores the current power of 10
    // and num is to store the number
    // which is appended in the end
    int num = 0, pw = 1;
    int i = n - 1;
    while (i >= 0)
    {
  
        // If current character is
        // a numeric digit
        if (s.charAt(i) >= '0' && 
            s.charAt(i) <= '9') 
        {
  
            // Get the current digit
            int digit = s.charAt(i) - '0';
  
            // Build the number
            num = num + (pw * digit);
  
            // If number exceeds the length
            if (num >= s_len)
                return -1;
  
            // Next power of 10
            pw = pw * 10;
        }
        i--;
    }
  
    // Append 0 in the end
    num = num * 10;
  
    // Required number that must be added
    int req = s_len - num;
  
    // If number is not a single digit
    if (req > 9 || req < 0)
        return -1;
    return req;
}
  
// Driver code
public static void main (String[] args)
{
    String s = "abcd0";
    int n = s.length();
      
    System.out.print(find_digit(s, n));
}
}
  
// This code is contributed by vt_m


Python3
# Python3 implementation of the approach
  
# Function to return the requried digit
def find_digit(s, n):
  
    # To store the position of the first
    # numeric digit in the string
    first_digit = -1
    for i in range(n - 1, -1, -1):
        if s[i] < '0' or s[i] > '9':
            first_digit = i
            break
  
    first_digit += 1
  
    # To store the length of the
    # string without the numeric
    # digits in the end
    s_len = first_digit
    num = 0
    pw = 1
    i = n - 1
    while i >= 0:
  
        # If current character is
        # a numeric digit
        if s[i] >= '0' and s[i] <= '9':
  
            # Get the current digit
            digit = ord(s[i]) - ord('0')
  
            # Build the number
            num = num + (pw * digit)
  
            # If number exceeds the length
            if num >= s_len:
                return -1
  
            # Next power of 10
            pw = pw * 10
  
        i -= 1
  
    # Append 0 in the end
    num = num * 10
  
    # Required number that must be added
    req = s_len - num
  
    # If number is not a single digit
    if req > 9 or req < 0:
        return -1
    return req
  
# Driver code
if __name__ == "__main__":
    s = "abcd0"
    n = len(s)
    print(find_digit(s, n))
  
# This code is contributed by
# sanjeev2552


C#
// C# implementation of the approach
using System;
      
class GFG 
{
  
// Function to return the requried digit
static int find_digit(String s, int n)
{
  
    // To store the position of the first
    // numeric digit in the string
    int first_digit = -1, i;
    for (i = n - 1; i >= 0; i--)
    {
        if (s[i] < '0' || 
            s[i] > '9') 
        {
            first_digit = i;
            break;
        }
    }
      
    first_digit++;
  
    // To store the length of the
    // string without the numeric
    // digits in the end
    int s_len = first_digit;
  
    // pw stores the current power of 10
    // and num is to store the number
    // which is appended in the end
    int num = 0, pw = 1;
    i = n - 1;
    while (i >= 0)
    {
  
        // If current character is
        // a numeric digit
        if (s[i] >= '0' && 
            s[i] <= '9') 
        {
  
            // Get the current digit
            int digit = s[i] - '0';
  
            // Build the number
            num = num + (pw * digit);
  
            // If number exceeds the length
            if (num >= s_len)
                return -1;
  
            // Next power of 10
            pw = pw * 10;
        }
        i--;
    }
  
    // Append 0 in the end
    num = num * 10;
  
    // Required number that must be added
    int req = s_len - num;
  
    // If number is not a single digit
    if (req > 9 || req < 0)
        return -1;
    return req;
}
  
// Driver code
public static void Main (String[] args)
{
    String s = "abcd0";
    int n = s.Length;
      
    Console.Write(find_digit(s, n));
}
}
  
// This code is contributed by PrinciRaj1992


输出:
4