给定一个仅包含两种数字6和9的正整数N ,任务是通过反转最多一位数字来生成可能的最小数字,即9变为6 ,反之亦然。
例子:
Input : 9996
Output : 6996
Explanation :
Changing the first digit results in 6996.
Changing the second digit results in 9696.
Changing the third digit results in 9966.
Changing the fourth digit results in 9999.
Hence, the minimum number among all possibilities is 6996.
Input : 6696
Output : 6666
Explanation :
Changing the first digit results in 9696.
Changing the second digit results in 6996.
Changing the third digit results in 6666.
Changing the fourth digit results in 6699.
Hence, the minimum number among all possibilities is 6666.
方法:
为了解决这个问题,我们需要按照下面给出的步骤:
- 首先将给定的整数转换为字符串。
- 从左侧遍历字符串并将第一次出现的 ‘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)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live