给定一个包含N个元素的数组。每个元素可以为0或5。找到可以被该数组中的任意数量的元素以任何方式排列的最大整数90。
例子:
Input : arr[] = {5, 5, 5, 5, 5, 5, 5, 5, 0, 5, 5}
Output : 5555555550
Input : arr[] = {5, 0}
Output : 0
由于我们可以选择和置换任意数量的元素,因此数组中只有0和5的数量很重要。因此,我们将计数分别存储为c0和c5。
该数字必须是90的倍数,即9 * 10。因此,该数字必须是9和10的倍数。
可除法规则如下:
- 对于要被10整除的数字,它应该以0结尾。
- 对于要被9整除的数字,数字总和应该被9整除。由于允许使用的唯一非零数字是5,因此我们使用5的次数必须是9的倍数,因此总和将是45的倍数,即被9整除。
有3种可能性:
- c0 = 0。这意味着不能将数字除以10。
- c5 = 0。这意味着可以被90整除的唯一数字是0。
- 如果以上两个条件都为假。让我们将5s分组为9个组。将要被完全填充的floor(c5 / 9)个组,我们可以使用所有组中的所有5s来获得5s的9的倍数,其中也会使数字总和为9的倍数。由于增加零的数量不会影响除数,因此我们可以使用所有零。
下面是上述方法的实现:
C++
// CPP program to find largest number
// divisible by 90 that can be made
// using 0 and 5
#include
using namespace std;
// Function to find largest number
// divisible by 90 that can be made
// using 0 and 5
void printLargestDivisible(int n, int a[])
{
// Count of 0s and 5s
int i, c0 = 0, c5 = 0;
for (i = 0; i < n; i++) {
if (a[i] == 0)
c0++;
else
c5++;
}
// The number of 5s that can be used
c5 = floor(c5 / 9) * 9;
if (c0 == 0) // The number can't be
cout << -1; // made multiple of 10
else if (c5 == 0) // The only multiple of 90
cout << 0; // that can be made is 0
else {
for (i = 0; i < c5; i++)
cout << 5;
for (i = 0; i < c0; i++)
cout << 0;
}
}
// Driver Code
int main()
{
int a[] = { 5, 5, 5, 5, 5, 5, 5, 5, 0, 5, 5 };
int n = sizeof(a) / sizeof(a[0]);
printLargestDivisible(n, a);
return 0;
}
Java
// Java program to find largest number
// divisible by 90 that can be made
// using 0 and 5
import java.io.*;
class GFG {
// Function to find largest number
// divisible by 90 that can be made
// using 0 and 5
static void printLargestDivisible(int n, int a[])
{
// Count of 0s and 5s
int i, c0 = 0, c5 = 0;
for (i = 0; i < n; i++) {
if (a[i] == 0)
c0++;
else
c5++;
}
// The number of 5s that can be used
c5 = (int)Math.floor(c5 / 9) * 9;
if (c0 == 0) // The number can't be
System.out.print(-1); // made multiple of 10
else if (c5 == 0) // The only multiple of 90
System.out.println(0); // that can be made is 0
else {
for (i = 0; i < c5; i++)
System.out.print(5);
for (i = 0; i < c0; i++)
System.out.print(0);
}
}
// Driver Code
public static void main (String[] args) {
int a[] = { 5, 5, 5, 5, 5, 5, 5, 5, 0, 5, 5 };
int n = a.length;
printLargestDivisible(n, a);
}
}
// This code is contributed
// by shs
Python3
# Python3 program to find largest number
# divisible by 90 that can be made
# using 0 and 5
# from math import every methods
from math import *
# Function to find largest number
# divisible by 90 that can be made
# using 0 and 5
def printLargestDivisible(n, a) :
# Count of 0s and 5s
c0, c5 = 0, 0
for i in range(n) :
if a[i] == 0 :
c0 += 1
else :
c5 += 1
# The number of 5s that can be used
c5 = floor(c5 / 9) * 9
if c0 == 0 : # The number can't be
print(-1,end = "") # made multiple of 10
elif c5 == 0 : # The only multiple of 90
print(0,end = "") # that can be made is 0
else :
for i in range(c5) :
print(5,end = "")
for i in range(c0) :
print(0, end = "")
# Driver code
if __name__ == "__main__" :
a = [ 5, 5, 5, 5, 5, 5, 5, 5, 0, 5, 5]
n = len(a)
# Function calling
printLargestDivisible(n, a)
# This code is contributed by
# ANKITRAI1
C#
// C# program to find largest number
// divisible by 90 that can be made
// using 0 and 5
using System;
class GFG {
// Function to find largest number
// divisible by 90 that can be made
// using 0 and 5
public static void printLargestDivisible(int n,
int[] a)
{
// Count of 0s and 5s
int i, c0 = 0, c5 = 0;
for (i = 0; i < n; i++)
{
if (a[i] == 0)
{
c0++;
}
else
{
c5++;
}
}
// The number of 5s that can be used
c5 = (c5 / 9) * 9;
// The number can't be
if (c0 == 0)
{
// made multiple of 10
Console.Write(-1);
}
// The only multiple of 90
else if (c5 == 0)
{
// that can be made is 0
Console.WriteLine(0);
}
else
{
for (i = 0; i < c5; i++)
{
Console.Write(5);
}
for (i = 0; i < c0; i++)
{
Console.Write(0);
}
}
}
// Driver Code
public static void Main(string[] args)
{
int[] a = new int[] {5, 5, 5, 5, 5,
5, 5, 5, 0, 5, 5};
int n = a.Length;
printLargestDivisible(n, a);
}
}
// This code is contributed by Shrikant13
PHP
输出:
5555555550
时间复杂度: O(N),其中N是数组中元素的数量。