-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwocakes.cpp
59 lines (40 loc) · 2.13 KB
/
twocakes.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <vector>
using namespace std;
int n, a, b;
/*
* It's New Year's Eve soon, so Ivan decided it's high time he started setting the table. Ivan has bought two cakes and cut them into pieces: the first cake has been cut into a pieces, and the second one — into b pieces.
Ivan knows that there will be n people at the celebration (including himself), so Ivan has set n plates for the cakes. Now he is thinking about how to distribute the cakes between the plates. Ivan wants to do it in such a way that all following conditions are met:
Each piece of each cake is put on some plate;
Each plate contains at least one piece of cake;
No plate contains pieces of both cakes.
To make his guests happy, Ivan wants to distribute the cakes in such a way that the minimum number of pieces on the plate is maximized. Formally, Ivan wants to know the maximum possible number x such that he can distribute the cakes according to the aforementioned conditions, and each plate will contain at least x pieces of cake.
Help Ivan to calculate this number x!
Input
The first line contains three integers n, a and b (1 ≤ a, b ≤ 100, 2 ≤ n ≤ a + b) — the number of plates, the number of pieces of the first cake, and the number of pieces of the second cake, respectively.
Output
Print the maximum possible number x such that Ivan can distribute the cake in such a way that each plate will contain at least x pieces of cake.
*/
int get_difference(int index){
int numberSlotsA = index+1;
int numberSlotsB = n - (index+1);
if(numberSlotsB == 0)return -1;
int divideA = a/numberSlotsA;
int divideB = b/numberSlotsB;
return min(divideA, divideB);
}
int main(){
cin >> n >> a >> b;
int safeIndex = -1 , lessDifference=-1 ,temp;
for(int i =0; i<n; i++){
temp = get_difference(i);
if(temp == -1) break;
if (lessDifference < temp || lessDifference == -1) {
lessDifference = temp;
safeIndex = i;
}
}
int divideA = a/ (safeIndex+1), divideB = b/(n -(safeIndex+1));
cout << min(divideA, divideB) << endl;
return 0;
}