public class Coconut {
/**
* Valiables
*/
/**
* Methods
*/
public static void main(String[] args) {
int people=5;
int coconuts=1;
System.out.println(people+"个人分椰子,每人分得:"+coconuts+"个,则,原来有椰子:"+peopleShareTheCokernut(people,coconuts));
}
/**
* 第一种算法
* */
static int peopleShareTheCokernut(int people,int cokernuts ){
int sum=cokernuts;
//保证每人要有椰子,则最后一次分椰子后至少有people个椰子,
//所以要循环people+1次,才能使得椰子足够
for(int i=0;i<=people;i++){
sum=sum*people;
}
//每分一次给猴子一个,最后一次没给,就是people-1个
sum-=(people-1);
return sum;
}
/**
* 另一种
* */
}
[创新杯]分椰子啥的.....
最新推荐文章于 2021-10-18 00:41:49 发布