给定一个非常大的数字num(1 <= num <= 10 ^ 1000),请打印需要删除的数字以使该数字可以被3整除。如果不能,请打印-1。
例子 :
Input: num = "1234"
Output: 1
Explanation: we need to remove one
digit that is 1 or 4, to make the
number divisible by 3.on
Input: num = "11"
Output: -1
Explanation: It is not possible to
remove any digits and make it divisible
by 3.
这个想法基于这样一个事实:当且仅当其数字的总和是3的倍数时,该数字才是3的倍数(有关详细信息,请参见此内容)。
此处使用的一项重要观察结果是,如果存在答案,则答案最多为2。因此,这是该函数的唯一选项:
- 数字总和已经等于0模3。因此,我们不必擦除任何数字。
- 存在等于总和模3的数字。然后我们只需要擦除一个数字即可。
- 所有数字都不能被3整除,也不等于模3的总和。因此,两个这样的数字总和等于数字,等于模3的总和,(2 + 2)mod 3 = 1,(1 + 1)mod 3 = 2
C++
// CPP program to find the minimum number of
// digits to be removed to make a large number
// divisible by 3.
#include
using namespace std;
// function to count the no of removal of digits
// to make a very large number divisible by 3
int divisible(string num)
{
int n = num.length();
// add up all the digits of num
int sum = accumulate(begin(num),
end(num), 0) - '0' * 1;
// if num is already is divisible by 3
// then no digits are to be removed
if (sum % 3 == 0)
return 0;
// if there is single digit, then it is
// not possible to remove one digit.
if (n == 1)
return -1;
// traverse through the number and find out
// if any number on removal makes the sum
// divisible by 3
for (int i = 0; i < n; i++)
if (sum % 3 == (num[i] - '0') % 3)
return 1;
// if there are two numbers then it is
// not possible to remove two digits.
if (n == 2)
return -1;
// Otherwise we can always make a number
// multiple of 2 by removing 2 digits.
return 2;
}
// Driver Code
int main()
{
string num = "1234";
cout << divisible(num);
return 0;
}
Java
// Java program to find the
// minimum number of digits
// to be removed to make a
// large number divisible by 3.
import java.io.*;
// function to count the no
// of removal of digits
// to make a very large
// number divisible by 3
class GFG {
static int divisible(String num)
{
int n = num.length();
// add up all the
// digits of num
int sum = 0;
for (int i = 0; i < n; i++)
sum += (int)(num.charAt(i));
// if num is already is
// divisible by 3 then
// no digits are to be
// removed
if (sum % 3 == 0)
return 0;
// if there is single digit,
// then it is not possible
// to remove one digit.
if (n == 1)
return -1;
// traverse through the number
// and find out if any number
// on removal makes the sum
// divisible by 3
for (int i = 0; i < n; i++)
if (sum % 3 == (num.charAt(i) - '0') % 3)
return 1;
// if there are two numbers
// then it is not possible
// to remove two digits.
if (n == 2)
return -1;
// Otherwise we can always
// make a number multiple
// of 2 by removing 2 digits.
return 2;
}
// Driver Code
public static void main(String[] args)
{
String num = "1234";
System.out.println(divisible(num));
}
}
// This code is contributed by Raj
Python3
# Python3 program to find the
# minimum number of digits to
# be removed to make a large
# number divisible by 3.
# function to count the
# no of removal of digits
# to make a very large
# number divisible by 3
def divisible(num):
n = len(num)
# add up all the digits of num
sum_ = 0
for i in range(n):
sum_ += int(num[i])
# if num is already is
# divisible by 3 then no
# digits are to be removed
if (sum_ % 3 == 0):
return 0
# if there is single digit,
# then it is not possible
# to remove one digit.
if (n == 1):
return -1
# traverse through the number
# and find out if any number
# on removal makes the sum
# divisible by 3
for i in range(n):
if (sum_ % 3 == int(num[i]) % 3):
return 1
# if there are two numbers
# then it is not possible
# to remove two digits.
if (n == 2):
return -1
# Otherwise we can always
# make a number multiple of
# 2 by removing 2 digits.
return 2
# Driver Code
if __name__ == '__main__':
num = "1234"
print(divisible(num))
# This code is contributed by mits
C#
// C# program to find the
// minimum number of digits
// to be removed to make a
// large number divisible by 3.
using System;
// function to count the no
// of removal of digits
// to make a very large
// number divisible by 3
class GFG {
static int divisible(String num)
{
int n = num.Length;
// add up all the
// digits of num
int sum = 0;
for (int i = 0; i < n; i++)
sum += (int)(num[i]);
// if num is already is
// divisible by 3 then
// no digits are to be
// removed
if (sum % 3 == 0)
return 0;
// if there is single digit,
// then it is not possible
// to remove one digit.
if (n == 1)
return -1;
// traverse through the number
// and find out if any number
// on removal makes the sum
// divisible by 3
for (int i = 0; i < n; i++)
if (sum % 3 == (num[i] - '0') % 3)
return 1;
// if there are two numbers
// then it is not possible
// to remove two digits.
if (n == 2)
return -1;
// Otherwise we can always
// make a number multiple
// of 2 by removing 2 digits.
return 2;
}
// Driver Code
public static void Main()
{
string num = "1234";
Console.WriteLine(divisible(num));
}
}
// This code is contributed by mits
PHP
输出 :
1
时间复杂度: O(n),其中n是数字的长度。