给定切割次数,找到可能的最大数量。
例子:
Input : 2
Output : 4
Input : 3
Output : 7
这个问题只不过是懒惰的餐饮业者的问题,并具有以下公式。
最大件数 = 1 + n*(n+1)/2
参考这个证明。
C++
// C++ program to find maximum no of pieces
// by given number of cuts
#include
using namespace std;
// Function for finding maximum pieces
// with n cuts.
int findMaximumPieces(int n)
{
return 1 + n*(n+1)/2;
}
// Driver code
int main()
{
cout << findMaximumPieces(3);
return 0;
}
Java
// Java program to find maximum no of
// pieces by given number of cuts
class GFG {
// Function for finding maximum pieces
// with n cuts.
static int findMaximumPieces(int n)
{
return 1 + n * (n + 1) / 2;
}
// Driver Program to test above function
public static void main(String arg[])
{
System.out.print(findMaximumPieces(3));
}
}
// This code is contributed by Anant Agarwal.
Python3
# Python3 program to find maximum
# no. of pieces by given
# number of cuts
# Function for finding maximum
# pieces with n cuts.
def findMaximumPieces(n):
return int(1 + n * (n + 1) / 2)
# Driver code
print(findMaximumPieces(3))
# This code is contributed 29AjayKumar
C#
// C# program to find maximum no of
// pieces by given number of cuts
using System;
class GFG {
// Function for finding maximum pieces
// with n cuts.
static int findMaximumPieces(int n)
{
return 1 + n * (n + 1) / 2;
}
// Driver Program to test above function
public static void Main()
{
Console.Write(findMaximumPieces(3));
}
}
// This code is contributed by nitin mittal.
PHP
Javascript
输出:
7
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。