给定N个数字(1、2、3,…. N)和一个数字S。任务是打印总和为S的最小数量的数字。
例子:
Input: N = 5, S = 11
Output: 3
Three numbers (smaller than or equal to N) can be any of the given combinations.
(3, 4, 4)
(2, 4, 5)
(1, 5, 5)
(3, 3, 5)
Input: N = 1, S = 10
Output: 10
方法:尽可能地选择N,然后如果剩余少于N的数字,我们将选择该数字加起来得到S,因此,数字的总数为(S / N)+1(如果S %N> 0) 。
下面是上述方法的实现。
C++
// C++ program to find the minimum numbers
// required to get to S
#include
using namespace std;
// Function to find the minimum
// numbers required to get to S
int minimumNumbers(int n, int s)
{
if (s % n)
return s / n + 1;
else
return s / n;
}
// Driver Code
int main()
{
int n = 5;
int s = 11;
cout << minimumNumbers(n, s);
return 0;
}
Java
// Java program to find the minimum numbers
// required to get to S
import java.io.*;
class GFG {
// Function to find the minimum
// numbers required to get to S
static int minimumNumbers(int n, int s)
{
if ((s % n)>0)
return s / n + 1;
else
return s / n;
}
// Driver Code
public static void main (String[] args) {
int n = 5;
int s = 11;
System.out.println(minimumNumbers(n, s));
}
}
// This code is contributed by shs..
Python 3
# Python 3 program to find the
# minimum numbers required to get to S
# Function to find the minimum
# numbers required to get to S
def minimumNumbers(n, s):
if (s % n):
return s / n + 1;
else:
return s / n;
# Driver Code
n = 5;
s = 11;
print(int(minimumNumbers(n, s)));
# This code is contributed
# by Shivi_Aggarwal
C#
// C# program to find the minimum numbers
// required to get to S
using System;
class GFG {
// Function to find the minimum
// numbers required to get to S
static int minimumNumbers(int n, int s)
{
if ((s % n)>0)
return s / n + 1;
else
return s / n;
}
// Driver Code
public static void Main () {
int n = 5;
int s = 11;
Console.WriteLine(minimumNumbers(n, s));
}
}
// This code is contributed by shs..
PHP
输出:
3