📌  相关文章
📜  通过最多更改一位数字找到可能的最小数字

📅  最后修改于: 2021-04-23 06:25:25             🧑  作者: Mango

给定一个仅由两种类型的数字69组成的正整数N ,任务是通过反转最多一位来生成最小数,即9变为6 ,反之亦然。

例子:

方法:
为了解决该问题,我们需要遵循以下步骤:

  • 首先将给定的整数转换为字符串。
  • 从左侧遍历字符串,并将第一个出现的“ 9”更改为“ 6”,然后退出循环。如果初始字符串没有出现9,那么它已经是可能的最低数字。
  • 将最终字符串转换回整数并打印。

下面是上述方法的实现:

C++
// C++ implementation to change at most
// one digit to make the number
// as minimum as possible
  
#include 
using namespace std;
  
// Function to return the minimum 
// possible number
int minimum69Number(int num)
{
  
    // Converting given number to string
    string s_num = to_string(num);
  
    // Traversing the string
    for (auto& c : s_num) {
        // change first 9 to 6
        if (c == '9') {
            c = '6';
            break;
        }
    }
  
    // Change the string back to the integer
    int result = stoi(s_num);
  
    // Return the final result
    return result;
}
  
// Driver code
int main()
{
    // Input number
    int n = 9996;
  
    int result = minimum69Number(n);
  
    // Print the result
    cout << result << endl;
}


Java
// Java implementation to change at most
// one digit to make the number
// as minimum as possible
class GFG{
  
// Function to return the minimum 
// possible number
static int minimum69Number(int num)
{
  
    // Converting given number to String
    char []s_num = String.valueOf(num).toCharArray();
      
    // Traversing the String
    for(int i = 0; i < s_num.length; i++)
    {
  
       // change first 9 to 6
       if (s_num[i] == '9')
       {
           s_num[i] = '6';
           break;
       }
    }
  
    // Change the String back to the integer
    int result = Integer.valueOf(String.valueOf(s_num));
  
    // Return the final result
    return result;
}
  
// Driver code
public static void main(String[] args)
{
  
    // Input number
    int n = 9996;
    int result = minimum69Number(n);
  
    // Print the result
    System.out.print(result + "\n");
}
}
  
// This code is contributed by 29AjayKumar


Python3
# Python3 implementation to change at most
# one digit to make the number
# as minimum as possible
  
# Function to return the minimum 
# possible number
def minimum69Number(num):
      
    # Converting given number to string
    s_num = str(num)
      
    s_num = s_num.replace('9','6', 1)
  
    # Change the string back to the integer
    result = int(s_num)
  
    # Return the final result
    return result
  
# Driver code
if __name__ == '__main__':
      
    # Input number
    n = 9996
      
    result = minimum69Number(n)
  
    # Print the result
    print(result)
  
# This code is contributed by Samarth


C#
// C# implementation to change at most
// one digit to make the number
// as minimum as possible
using System;
  
class GFG{
  
// Function to return the minimum 
// possible number
static int minimum69Number(int num)
{
  
    // Converting given number to String
    char []s_num = String.Join("", num).ToCharArray();
      
    // Traversing the String
    for(int i = 0; i < s_num.Length; i++)
    {
       // change first 9 to 6
       if (s_num[i] == '9')
       {
           s_num[i] = '6';
           break;
       }
    }
  
    // Change the String back to the integer
    int result = Int32.Parse(String.Join("", s_num));
  
    // Return the readonly result
    return result;
}
  
// Driver code
public static void Main(String[] args)
{
  
    // Input number
    int n = 9996;
    int result = minimum69Number(n);
  
    // Print the result
    Console.Write(result + "\n");
}
}
  
// This code is contributed by sapnasingh4991


输出:
6996

时间复杂度: O(n)

辅助空间: O(1)