-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSchoolAssembly.java
More file actions
56 lines (48 loc) · 989 Bytes
/
Copy pathSchoolAssembly.java
File metadata and controls
56 lines (48 loc) · 989 Bytes
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
public class SchoolAssembly
{
public static int getBeans(int kids, int quantity)
{
return (kids * quantity + 4 * (quantity - 1) + 19) / 20;
}
public static int getBeans2(int kids, int quantity)
{
int [] buckets = new int [5];
int len = 5;
int q = quantity - 1;
/* Initially fill all buckets with quantity - 1 */
for(int i = 0; i < len; i++)
{
buckets[i] += q;
}
int beans = len * q;
int sets = 0;
boolean flag = false;
while(sets < kids)
{
for(int i = 0; i < len; i++)
{
/* If the number of beans in the buckets aren't yet quantity - 1 */
if(buckets[i] + 1 % quantity != 0)
{
buckets[i]++;
flag = true;
break;
}
}
if(!flag)
{
buckets[Math.random() * 4] = 0;
sets++;
}
/* Reset the flag and increment the number of beans */
beans++;
flag = false;
}
}
public static void main(String[]args)
{
int kids = 5;
int quantity = 5;
System.out.println(getBeans(5, 5));
}
}