给定一个整数N ,任务是在给定整数上应用两次给定操作后找到最大差异。
操作定义如下:
- 从N 中选择任何数字(0-9)并将相同数字的所有实例替换为任何其他数字(0-9) 。
- 运算后的N不能有前导零,也不能等于零。
例子:
Input : N = 777
Output : 888
Explanation: Select digit 7 and replace all it’s occurrences with 9 to get 999. similarly 111 can be obtained by replacing all occurrences of 7 with 1. Hence, the possible maximum difference is 888.
Input : N = 123456
Output : 820000
The numbers after two operation can be 923456 and 103456. Hence, the required maximum difference is 820000.
说明:最大差可以通过对N的给定运算得到的最大值和最小值相减得到。
- 的最大数量可以通过从它不是左采摘N的第一个数字等于“9”和替换数字的所有实例为“9”来获得。
- 找到最小数有点棘手,因为N不能有任何前导零,也不能等于零。如果N左边的第一个数字是‘1’ ,则找到左边第一个大于‘1’的数字,并将该数字的所有实例替换为‘0’ 。
- 否则,如果N左边的第一个数字不等于‘1’ ,则选择该数字并将该数字的所有实例替换为‘1’ 。
- 最后,返回最小和最大数之间的差作为答案。
下面是上述方法的实现:
C++
// C++ program to find the
// maximum difference
// after two given operations
// on a number
#include
using namespace std;
// Function that returns the
// maximum difference
// after two given operations
// on a number
int minDifference(int num)
{
// Initialize the strings to make operations
string maximum = to_string(num);
string minimum = to_string(num);
// Store length of the string
int n = maximum.size();
// Make the maximum number using
// the first operation
for (int i = 0; i < n; i++) {
// Find the first digit which
// is not equal to '9'
if (maximum[i] != '9') {
// Store the digit for
// the given operation
char digit = maximum[i];
for (int j = i; j < n; j++) {
// Replace all the selected
// 'digit' with '9'
if (maximum[j] == digit)
maximum[j] = '9';
}
// Break after the operation
// is completed
break;
}
}
// Make the minimum number using
// the second operation
// Check if first digit is equal to '1'
if (minimum[0] == '1') {
for (int i = 1; i < n; i++) {
// Find the first digit which
// is greater than '1'
if (minimum[i] - '0' > 1) {
// Store the digit for
// the given operation
char digit = minimum[i];
for (int j = i; j < n; j++) {
// Replace all the selected
// 'digit' with '0'
if (digit == minimum[j])
minimum[j] = '0';
}
// Break after the
// operation is completed
break;
}
}
}
else {
// Select the first digit for
// the given operation
char digit = minimum[0];
for (int i = 0; i < n; i++) {
// Replace all the selected
// 'digit' with '1'
if (minimum[i] == digit)
minimum[i] = '1';
}
}
// Return the difference after
// converting into integers
return (stoi(maximum)
- stoi(minimum));
}
// Driver Code
int main()
{
int N = 1101157;
cout << minDifference(N);
return 0;
}
Java
// Java program to find the maximum
// difference after two given operations
// on a number
import java.util.*;
class GFG{
// Function that returns the
// maximum difference
// after two given operations
// on a number
static int minDifference(int num)
{
// Initialize the strings to make operations
StringBuilder maximum =
new StringBuilder(Integer.toString(num));
StringBuilder minimum =
new StringBuilder(Integer.toString(num));
// Store length of the string
int n = maximum.length();
// Make the maximum number using
// the first operation
for(int i = 0; i < n; i++)
{
// Find the first digit which
// is not equal to '9'
if (maximum.charAt(i) != '9')
{
// Store the digit for
// the given operation
char digit = maximum.charAt(i);
for(int j = i; j < n; j++)
{
// Replace all the selected
// 'digit' with '9'
if (maximum.charAt(j) == digit)
maximum.setCharAt(j, '9');
}
// Break after the operation
// is completed
break;
}
}
// Make the minimum number
// using the second operation
// Check if first digit is equal to '1'
if (minimum.charAt(0) == '1')
{
for(int i = 1; i < n; i++)
{
// Find the first digit which
// is greater than '1'
if (minimum.charAt(i) - '0' > 1)
{
// Store the digit for
// the given operation
char digit = minimum.charAt(i);
for(int j = i; j < n; j++)
{
// Replace all the selected
// 'digit' with '0'
if (digit == minimum.charAt(j))
minimum.setCharAt(j, '0');
}
// Break after the
// operation is completed
break;
}
}
}
else
{
// Select the first digit for
// the given operation
char digit = minimum.charAt(0);
for(int i = 0; i < n; i++)
{
// Replace all the selected
// 'digit' with '1'
if (minimum.charAt(i) == digit)
minimum.setCharAt(i, '1');
}
}
// Return the difference after
// converting into integers
return (Integer.parseInt(maximum.toString()) -
Integer.parseInt(minimum.toString()));
}
// Driver code
public static void main(String[] args)
{
int N = 1101157;
System.out.println(minDifference(N));
}
}
// This code is contributed by offbeat
Python3
# Python3 program to find the
# maximum difference after
# two given operations
# on a number
# Function that returns the
# maximum difference after
# two given operations
# on a number
def minDifference(num):
# Initialize the strings to
# make operations
maximum = list(str(num));
minimum = list(str(num));
# Store length of the string
n = len(maximum);
# Make the maximum number using
# the first operation
for i in range(n):
# Find the first digit which
# is not equal to '9'
if (maximum[i] != '9'):
# Store the digit for
# the given operation
digit = maximum[i];
for j in range(i, n):
# Replace all the selected
# 'digit' with '9'
if (maximum[j] == digit):
maximum[j] = '9';
# Break after the operation
# is completed
break;
# Make the minimum number using
# the second operation
# Check if first digit is equal to '1'
if (minimum[0] == '1'):
for i in range(1, n):
# Find the first digit which
# is greater than '1'
if (ord(minimum[i]) - ord('0') > 1):
# Store the digit for
# the given operation
digit = minimum[i];
for j in range(i, n):
# Replace all the selected
# 'digit' with '0'
if (digit == minimum[j]):
minimum[j] = '0';
# Break after the
# operation is completed
break;
else :
# Select the first digit
# for the given operation
digit = minimum[0];
for i in range(n):
# Replace all the selected
# 'digit' with '1'
if (minimum[i] == digit):
minimum[i] = '1';
maximum = "".join(maximum)
minimum = "".join(minimum)
# Return the difference after
# converting into integers
return (int(maximum) - int(minimum));
# Driver Code
if __name__ == "__main__":
N = 1101157;
print(minDifference(N));
# This code is contributed by AnkitRai01
C#
// C# program to find the maximum
// difference after two given
// operations on a number
using System;
using System.Collections.Generic;
class GFG{
// Function that returns the
// maximum difference after
// two given operations on a
// number
static int minDifference(int num)
{
// Initialize the strings to make operations
char[] maximum = (num.ToString()).ToCharArray();
char[] minimum = (num.ToString()).ToCharArray();
// Store length of the string
int n = maximum.Length;
// Make the maximum number using
// the first operation
for(int i = 0; i < n; i++)
{
// Find the first digit which
// is not equal to '9'
if (maximum[i] != '9')
{
// Store the digit for
// the given operation
char digit = maximum[i];
for(int j = i; j < n; j++)
{
// Replace all the selected
// 'digit' with '9'
if (maximum[j] == digit)
maximum[j] = '9';
}
// Break after the operation
// is completed
break;
}
}
// Make the minimum number using
// the second operation
// Check if first digit is equal to '1'
if (minimum[0] == '1')
{
for(int i = 1; i < n; i++)
{
// Find the first digit which
// is greater than '1'
if (minimum[i] - '0' > 1)
{
// Store the digit for
// the given operation
char digit = minimum[i];
for(int j = i; j < n; j++)
{
// Replace all the selected
// 'digit' with '0'
if (digit == minimum[j])
minimum[j] = '0';
}
// Break after the
// operation is completed
break;
}
}
}
else
{
// Select the first digit for
// the given operation
char digit = minimum[0];
for(int i = 0; i < n; i++)
{
// Replace all the selected
// 'digit' with '1'
if (minimum[i] == digit)
minimum[i] = '1';
}
}
// Return the difference after
// converting into integers
return Convert.ToInt32(string.Join("", maximum)) -
Convert.ToInt32(string.Join("", minimum));
}
// Driver Code
static void Main()
{
int N = 1101157;
Console.Write(minDifference(N));
}
}
// This code is contributed by divyesh072019
Javascript
输出:
8808850
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。