Alex is attending a Halloween party with his girlfriend, Silvia. At the party, Silvia spots the corner of an infinite chocolate bar (two-dimensional, infinitely long in width and length).
If the chocolate can be served only as 1 x 1 sized pieces and Alex can cut the chocolate bar exactly K times, what is the maximum number of chocolate pieces Alex can cut and give Silvia?
Example:
Input: k = 5
Output: 6
Approach
Java
public class HalloweanParty {public static void main(String[] args) {int k = 5;System.out.println(halloweenParty(k));}static long halloweenParty(int k) {long y1 = k / 2, y2;if (k % 2 == 0)y2 = k / 2;elsey2 = k / 2 + 1;return y1 * y2;}}
C++
#include <bits/stdc++.h>using namespace std;long halloweenParty(int k){long y1 = k / 2, y2;if (k % 2 == 0)y2 = k / 2;elsey2 = k / 2 + 1;return y1 * y2;}int main(){int k = 5;cout << halloweenParty(k) << "\n";return 0;}
No comments:
Post a Comment