📜  获得给定和 S 所需的最小十进制数

📅  最后修改于: 2021-10-26 06:48:24             🧑  作者: Mango

给定一个数字字符串s表示正十进制整数,任务是找正套十倍二进制数的最小数目,以获得和S要求。

例子:

方法:根据以下观察可以解决给定的问题:

因此,为了解决这个问题,迭代字符串S的字符并找到其中存在的最大数字。

下面是上述方法的实现:

C++
// C++ Program to implemeent
// the above approach
 
#include 
using namespace std;
 
// Function to find the count of minimum
// Deci-Binary numbers required to obtain S
int minimum_deci_binary_number(string s)
{
    // Stores the minimum count
    int m = INT_MIN;
 
    // Iterate over the string s
    for (int i = 0; i < s.size(); i++) {
 
        // Convert the char to its
        // equivalent integer
        int temp = s[i] - '0';
 
        // If current character is
        // the maximum so far
        if (temp > m) {
 
            // Update the maximum digit
            m = temp;
        }
    }
 
    // Print the required result
    return m;
}
 
// Driver Code
int main()
{
 
    string S = "31";
    cout << minimum_deci_binary_number(S);
 
    return 0;
}


Java
// Java program to implemeent
// the above approach
class GFG{
     
// Function to find the count of minimum
// Deci-Binary numbers required to obtain S
static int minimum_deci_binary_number(String s)
{
     
    // Stores the minimum count
    int m = Integer.MIN_VALUE;
 
    // Iterate over the string s
    for(int i = 0; i < s.length(); i++)
    {
         
        // Convert the char to its
        // equivalent integer
        int temp = s.charAt(i) - '0';
 
        // If current character is
        // the maximum so far
        if (temp > m)
        {
             
            // Update the maximum digit
            m = temp;
        }
    }
     
    // Print the required result
    return m;
}
 
// Driver Code
public static void main (String[] args)
{
    String S = "31";
     
    System.out.println(minimum_deci_binary_number(S));
}
}
 
// This code is contributed by AnkThon


Python3
# Python3 Program to implemeent
# the above approach
 
# Function to find the count of minimum
# Deci-Binary numbers required to obtain S
def minimum_deci_binary_number(s):
     
    # Stores the minimum count
    m = -10**19
 
    # Iterate over the string s
    for i in range(len(s)):
 
        # Convert the char to its
        # equivalent integer
        temp = ord(s[i]) - ord('0')
 
        # If current character is
        # the maximum so far
        if (temp > m):
 
            # Update the maximum digit
            m = temp
 
    # Prthe required result
    return m
 
# Driver Code
if __name__ == '__main__':
 
    S = "31"
    print(minimum_deci_binary_number(S))
 
# This code is contributed by mohit kumar 29


C#
// C# program to implemeent
// the above approach
using System;
class GFG
{
     
    // Function to find the count of minimum
    // Deci-Binary numbers required to obtain S
    static int minimum_deci_binary_number(string s)
    {
         
        // Stores the minimum count
        int m = int.MinValue;
     
        // Iterate over the string s
        for(int i = 0; i < s.Length; i++)
        {
             
            // Convert the char to its
            // equivalent integer
            int temp = s[i] - '0';
     
            // If current character is
            // the maximum so far
            if (temp > m)
            {
                 
                // Update the maximum digit
                m = temp;
            }
        }
         
        // Print the required result
        return m;
    }
     
    // Driver Code
    public static void Main (String[] args)
    {
        string S = "31";       
        Console.WriteLine(minimum_deci_binary_number(S));
    }
}
 
// This code is contributed by AnkThon


Javascript


输出:
3

时间复杂度: O(N)
辅助空间: O(N)