给定三个非零数字0 。任务是找到可被所有{A,B,C}集的3整除的最小数字。
注意:不必全部包含三个数字。结果可以是A , AA , AB , CCA等。
例子:
Input: A = 2, B = 4, C = 7
Output: 24
24 is the minimum number divisible by 3 that can be formed by the given digits.
Input: A = 1, B = 2, C = 3
Output: 3
方法:将所有三个数字放在一个数组中,然后按升序对其进行排序。现在检查是否有任何数字可以被3整除,如果是,则该数字将成为答案。
如果不是,则再次使用两个数字中的任意一个来检查。最后取最小的数字,我们的结果是该数字重复三遍。
为什么我们不能得到长度超过三位数的答案?
即使任何数字不能被3整除,重复最小的3次也可以使它被3整除。请注意,如果数字的总和可以被3整除,那么该数字也可以被3整除。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the minimum number
// divisible by 3 formed by the given digits
int printSmallest(int a[3])
{
int sum, sum1;
// Sort the given array in ascending
sort(a, a + 3);
int i, j, k, num;
// Check if any single digit is divisible by 3
for (int i = 0; i < 3; i++) {
if (a[i] % 3 == 0)
return a[i];
}
// Check if any two digit number formed by
// the given digits is divisible by 3
// starting from the minimum
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
// Generate the two digit number
num = (a[i] * 10) + a[j];
if (num % 3 == 0)
return num;
}
}
// If none of the above is true, we can
// form three digit number by taking a[0]
// three times.
return a[0]*100 + a[0]*10 + a[0];
}
// Driver code
int main()
{
int arr[] = { 7, 7, 1 };
cout << printSmallest(arr);
return 0;
}
Java
// Java implementation of the approach
import java.util.Arrays;
public class GFG {
// Function to return the minimum number
// divisible by 3 formed by the given digits
static int printSmallest(int a[]) {
int sum, sum1;
// Sort the given array in ascending
Arrays.sort(a);
int i, j, k, num;
// Check if any single digit is divisible by 3
for (i = 0; i < 3; i++) {
if (a[i] % 3 == 0) {
return a[i];
}
}
// Check if any two digit number formed by
// the given digits is divisible by 3
// starting from the minimum
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
// Generate the two digit number
num = (a[i] * 10) + a[j];
if (num % 3 == 0) {
return num;
}
}
}
// If none of the above is true, we can
// form three digit number by taking a[0]
// three times.
return a[0] * 100 + a[0] * 10 + a[0];
}
// Driver code
public static void main(String[] args) {
int arr[] = {7, 7, 1};
System.out.println(printSmallest(arr));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 implementation of the approach
# Function to return the minimum
# number divisible by 3 formed by
# the given digits
def printSmallest(a, n):
sum0, sum1 = 0, 0
# Sort the given array in ascending
a = sorted(a)
# Check if any single digit is
# divisible by 3
for i in range(n):
if (a[i] % 3 == 0):
return a[i]
# Check if any two digit number
# formed by the given digits is
# divisible by 3 starting from
# the minimum
for i in range(n):
for j in range(n):
# Generate the two digit number
num = (a[i] * 10) + a[j]
if (num % 3 == 0):
return num
# If none of the above is true, we can
# form three digit number by taking a[0]
# three times.
return a[0] * 100 + a[0] * 10 + a[0]
# Driver code
arr = [7, 7, 1 ]
n = len(arr)
print(printSmallest(arr, n))
# This code is conteibuted
# by mohit kumar 29
C#
// C# implementation of the approach
using System;
public class GFG {
// Function to return the minimum number
// divisible by 3 formed by the given digits
static int printSmallest(int []a) {
// Sort the given array in ascending
Array.Sort(a);
int i, j, num;
// Check if any single digit is divisible by 3
for (i = 0; i < 3; i++) {
if (a[i] % 3 == 0) {
return a[i];
}
}
// Check if any two digit number formed by
// the given digits is divisible by 3
// starting from the minimum
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
// Generate the two digit number
num = (a[i] * 10) + a[j];
if (num % 3 == 0) {
return num;
}
}
}
// If none of the above is true, we can
// form three digit number by taking a[0]
// three times.
return a[0] * 100 + a[0] * 10 + a[0];
}
// Driver code
public static void Main() {
int []arr = {7, 7, 1};
Console.Write(printSmallest(arr));
}
}
//This code is contributed by Rajput-Ji
PHP
输出:
111