-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmelonSeed.java
More file actions
51 lines (36 loc) · 900 Bytes
/
Copy pathmelonSeed.java
File metadata and controls
51 lines (36 loc) · 900 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
// 吃一颗,剥两颗,每吃十颗,从剥的所有中拿出1/4吃掉
// 现在有100颗,问:吃了多少颗?
/*
循环吃10颗
总共吃的 = 每次循环吃的 + 没次循环吃10颗后的总共剩余的四分之一
10 20 20/4=5 15 20-5=15
16 17
17 19
18 21
*/
class melon
{
public static void main(String[] args)
{ int eat = totalEat(100);
System.out.println("I have ate "+eat+" melon seeds\n");
}
public static int totalEat(int totalResidue)
{
int eat = 0;
int residue = 0;
int eatTotal = 0;
while (residue < totalResidue)
{
eat++;
residue+=2;
eatTotal++;
if (10 == eat)
{
eatTotal += residue/4;
residue -= (residue/4);
eat = 0;
}
}
return eatTotal;
}
}