给定三种不同类型的杯子(a [])和碟子(b []),以及n个架子,请确定是否可以使杯子和架子整齐地布置。
如果遵循以下规则,则杯子和碟子的布置将变得整洁:
- 没有架子可以容纳杯子和碟子
- 每个架子上最多只能有5个杯子
- 每个架子上最多只能有10个碟子
例子:
Input : a[] = {3, 2, 6}
b[] = {4, 8, 9}
n = 10
Output : Yes
Explanation :
Total cups = 11, shelves required = 3
Total saucers = 21, shelves required = 3
Total required shelves = 3 + 3 = 6,
which is less than given number of
shelves n. So, output is Yes.
Input : a[] = {4, 7, 4}
b[] = {3, 9, 10}
n = 2
Output : No
方法:安排杯子和碟子,找出杯子总数和碟总数 。由于同一架子上的杯子不能超过5个,因此可以通过以下公式找出杯子所需的最大架子数并使用公式计算出碟子所需的最大架子数 。如果这两个值的总和等于或小于那么这种安排是可能的,否则是不可能的。
下面是上述方法的实现:
C++
// C++ code to find if neat
// arrangement of cups and
// shelves can be made
#include
using namespace std;
// Function to check arrangement
void canArrange(int a[], int b[], int n)
{
int suma = 0, sumb = 0;
// Calculating total number
// of cups
for(int i = 0; i < 3; i++)
suma += a[i];
// Calculating total number
// of saucers
for(int i = 0; i < 3; i++)
sumb += b[i];
// Adding 5 and 10 so that if the
// total sum is less than 5 and
// 10 then we can get 1 as the
// answer and not 0
int na = (suma + 5 - 1) / 5;
int nb = (sumb + 10 - 1) / 10;
if(na + nb <= n)
cout << "Yes";
else
cout << "No";
}
// Driver code
int main()
{
// Number of cups of each type
int a[] = {3, 2, 6};
// Number of saucers of each type
int b[] = {4, 8, 9};
// Number of shelves
int n = 10;
// Calling function
canArrange(a, b, n);
return 0;
}
Java
// Java code to find if neat
// arrangement of cups and
// shelves can be made
import java.io.*;
class Gfg
{
// Function to check arrangement
public static void canArrange(int a[], int b[],
int n)
{
int suma = 0, sumb = 0;
// Calculating total number
// of cups
for(int i = 0; i < 3; i++)
suma += a[i];
// Calculating total number
// of saucers
for(int i = 0; i < 3; i++)
sumb += b[i];
// Adding 5 and 10 so that if
// the total sum is less than
// 5 and 10 then we can get 1
// as the answer and not 0
int na = (suma + 5 - 1) / 5;
int nb = (sumb + 10 - 1) / 10;
if(na + nb <= n)
System.out.println("Yes");
else
System.out.println("No");
}
// Driver function
public static void main(String args[])
{
// Number of cups of each type
int a[] = {3, 2, 6};
// Number of saucers of each type
int b[] = {4, 8, 9};
// Number of shelves
int n = 10;
// Calling function
canArrange(a, b, n);
}
}
Python 3
# Python code to find if neat
# arrangement of cups and
# shelves can be made
import math
# Function to check arrangement
def canArrange( a, b, n):
suma = 0
sumb = 0
# Calculating total number
# of cups
for i in range(0, len(a)):
suma += a[i]
# Calculating total number
# of saucers
for i in range(0,len(b)):
sumb += b[i]
# Adding 5 and 10 so that if
# the total sum is less than
# 5 and 10 then we can get 1
# as the answer and not 0
na = (suma + 5 - 1) / 5
nb = (sumb + 10 - 1) / 10
if(na + nb <= n):
print("Yes")
else:
print("No")
# driver function
#Number of cups of each type
a = [3, 2, 6]
# Number of saucers of each type
b = [4, 8, 9]
# Number of shelves
n = 10
#Calling function
canArrange(a ,b ,n)
# This code is contributed by Gitanjali.
C#
// C# code to find if neat
// arrangement of cups and
// shelves can be made
using System;
class Gfg {
// Function to check arrangement
public static void canArrange(int []a, int []b,
int n)
{
int suma = 0, sumb = 0;
// Calculating total number
// of cups
for(int i = 0; i < 3; i++)
suma += a[i];
// Calculating total number
// of saucers
for(int i = 0; i < 3; i++)
sumb += b[i];
// Adding 5 and 10 so that if
// the total sum is less than
// 5 and 10 then we can get 1
// as the answer and not 0
int na = (suma + 5 - 1) / 5;
int nb = (sumb + 10 - 1) / 10;
if(na + nb <= n)
Console.WriteLine("Yes");
else
Console.WriteLine("No");
}
// Driver function
public static void Main()
{
// Number of cups of each type
int []a = {3, 2, 6};
// Number of saucers of each type
int []b = {4, 8, 9};
// Number of shelves
int n = 10;
// Calling function
canArrange(a, b, n);
}
}
// This code is contributed by vt_m.
PHP
Javascript
输出:
Yes
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。