在 N 中插入任何数字后可被 9 整除的最大整数
给定一个大整数N ,任务是在N中的任意位置插入从0到9的一个数字后,找到可被9整除的最大可能整数。
注意:不允许使用前导零。
例子:
Input: N = “12”
Output: 612
Explanation: The numbers which are divisible by 9 after inserting digit are 612, 162, 126.
And the largest integer is 612. So the output is 612
Input: N = “1346182944512412414214”
Output: 81346182944512412414214
方法:问题可以通过以下观察来解决:
The idea is to insert the digit 0 to 9 at every position in the N and check is it divisible by 9 (divisible only if sum of digits is divisible by 9).
If it is found to be true then store the maximum value with position where digit is inserted and finally print the maximum value.
请按照以下步骤解决问题:
- 初始化一个对变量(比如maxi = {“”, 0} )以存储可被9整除的最大整数和插入数字的位置。
- 遍历范围[0, len)并计算数字的数字总和(例如存储在变量sum中) 。
- 遍历范围[0, len)并执行以下步骤:
- 迭代ch = '0' 到 '9'并执行以下步骤:
- 检查前导0 s,如果发现为真,则继续迭代。
- 检查(ch – '0') + sum是否可被9整除(即在N中插入该数字后的总和)。 如果发现为真,则将maxi的值设置或更新为新值。
- 迭代ch = '0' 到 '9'并执行以下步骤:
- 最后,打印最大数的值(存储在maxi.first中)。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Fuction to check number is divisible by 9
bool isDivBy9(char c, int sum)
{
return (((c - '0') + sum) % 9 == 0);
}
// Fuction to find the largest number
pair maxNumber(pair
s1,
string s2, int i)
{
if ((s1.first[s1.second] - '0') > (s2[s1.second] - '0'))
return s1;
return { s2, i };
}
// Function to find the largest integer
// which is divisible by 9 after
// inserting any digit in the N
string findLargestNumber(string N, int len)
{
// Stores the largest integer which is
// divisible by 9
pair maxi = { "", 0 };
// Stores the sum of digits of N
int sum = 0;
for (int i = 0; i < len; i++) {
// Update the value of sum
sum += (N[i] - '0');
}
for (int i = 0; i <= len; i++) {
for (char ch = '0'; ch <= '9'; ch++) {
// Skip leading zeroes
if (i == 0 && ch == '0')
continue;
// Check number is divisible by 9
if (isDivBy9(ch, sum)) {
// Check maxi is not set
if (maxi.first.length() == 0) {
// Set value of maxi
maxi = {
N.substr(0, i) + ch + N.substr(i), i
};
}
else {
// Update value of maxi
maxi = maxNumber(maxi,
N.substr(0, i) + ch + N.substr(i), i);
}
}
}
}
// Print the value of maxi.first
return maxi.first;
}
// Driver Code
int main()
{
string N = "12";
int len = N.length();
// Function call
cout << findLargestNumber(N, len);
return 0;
}
Python3
# Python3 program for the above approach
# Fuction to check number is divisible by 9
def isDivBy9(c, sums):
return ((c + sums) % 9) == 0
# Fuction to find the largest number
def maxNumber(s1, s2, i):
if s1[0][s1[1]] > s2[s1[1]]:
return s1
return [s2, i]
# Function to find the largest integer
# which is divisible by 9 after
# inserting any digit in the N
def findLargestNumber(N, length):
# NOTE: we are using length as the variable name
# instead of len because len is a predefined method in Python3
# that we will be using in this program
# this is a good practice in code
# Stores the largest integer which is
# divisible by 9
maxi = ["", 0]
# Stores the sum of digits of N
sums = 0
for i in range(length):
# Update the value of sum
sums += ord(N[i]) - ord("0")
for i in range(length + 1):
for ch in range(10):
# Skip leading zeroes
if i == 0 and ch == 0:
continue
# Check number is divisible by 9
if isDivBy9(ch, sums):
# Check maxi is not set
if len(maxi[0]) == 0:
# Set value of maxi
maxi = [N[0:i] + str(ch) + N[i::], i]
else:
# Update value of maxi
maxi = maxNumber(maxi, N[0:i] + str(ch) + N[i:], i)
# Print the value of the first
# element of maxi
return maxi[0]
# Driver Code
N = "12"
length = len(N)
# Function call
print(findLargestNumber(N, length))
# This code is contributed by phasing17
Javascript
612
时间复杂度: O(N)
辅助空间: O(1)