📌  相关文章
📜  给定操作的最大可能数目

📅  最后修改于: 2021-04-28 16:48:31             🧑  作者: Mango

给定正整数N ,任务是通过更改数字,将该整数转换为最大可能的整数,且不使用前导零。如果X + Y = 9,则只能将数字X更改为数字Y。

例子:

方法:仅需要更改大于或等于5的数字,因为更改小于5的数字将导致更大的数字。在所有必需的数字均已更新后,检查结果数字是否带有前导零,如果是,则将其更改为9。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the maximum possible
// integer that can be obtained from the
// given integer after performing
// the given operations
string maxInt(string str)
{
    // For every digit
    for (int i = 0; i < str.length(); i++) {
  
        // Digits greater than or equal to 5
        // need not to be changed as changing
        // them will lead to a smaller number
        if (str[i] < '5') {
            str[i] = ('9' - str[i]) + '0';
        }
    }
  
    // The resulting integer
    // cannot have leading zero
    if (str[0] == '0')
        str[0] = '9';
  
    return str;
}
  
// Driver code
int main()
{
    string str = "42";
  
    cout << maxInt(str);
  
    return 0;
}


Java
// Java implementation of the approach 
class GFG 
{
  
    // Function to return the maximum possible 
    // integer that can be obtained from the 
    // given integer after performing 
    // the given operations 
    static String maxInt(char str[]) 
    { 
        // For every digit 
        for (int i = 0; i < str.length; i++)
        { 
      
            // Digits greater than or equal to 5 
            // need not to be changed as changing 
            // them will lead to a smaller number 
            if (str[i] < '5')
            { 
                str[i] = (char)(('9' - str[i]) + '0'); 
            } 
        } 
      
        // The resulting integer 
        // cannot have leading zero 
        if (str[0] == '0') 
            str[0] = '9'; 
      
        String str2 = new String(str);
        return str2; 
    } 
      
    // Driver code 
    public static void main (String[] args) 
    { 
        String str = "42"; 
      
        System.out.println(maxInt(str.toCharArray())); 
    } 
}
  
// This code is contributed by AnkitRai01


Python3
# Python3 implementation of the approach 
  
# Function to return the maximum possible 
# integer that can be obtained from the 
# given integer after performing 
# the given operations 
def maxInt(string): 
    string2 = ""
      
    # For every digit 
    for i in range(0, len(string)): 
  
        # Digits greater than or equal to 5 
        # need not to be changed as changing 
        # them will lead to a smaller number 
        if (string[i] < '5'):
            string2 += str((ord('9') - ord(string[i])))
        else:
            string2 += str(string[i])
              
    # The resulting integer 
    # cannot have leading zero 
    if (string2[0] == '0'): 
        string2[0] = '9'
  
    return string2 
  
# Driver code 
if __name__ == '__main__': 
  
    string = "42"
    print(maxInt(string)) 
  
# This code is contributed by ashutosh450


C#
// C# implementation of the approach
using System; 
  
class GFG 
{
  
    // Function to return the maximum possible 
    // integer that can be obtained from the 
    // given integer after performing 
    // the given operations 
    static String maxInt(char []str) 
    { 
        // For every digit 
        for (int i = 0; i < str.Length; i++)
        { 
      
            // Digits greater than or equal to 5 
            // need not to be changed as changing 
            // them will lead to a smaller number 
            if (str[i] < '5')
            { 
                str[i] = (char)(('9' - str[i]) + '0'); 
            } 
        } 
      
        // The resulting integer 
        // cannot have leading zero 
        if (str[0] == '0') 
            str[0] = '9'; 
      
        String str2 = new String(str);
        return str2; 
    } 
      
    // Driver code 
    public static void Main (String []args) 
    { 
        String str = "42"; 
      
        Console.WriteLine(maxInt(str.ToCharArray())); 
    } 
}
  
// This code is contributed by Arnab Kundu


输出:
57