62,614
社区成员
发帖
与我相关
我的任务
分享
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package peach;
/**
*
* @author Administrator
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
//第5只猴子拿到得桃子
int count=0;
for(int x=1;x<10000;x++){
boolean isInt=false;
count=5*x+1;//第5堆得桃子数量
for(int i=0;i<4;i++){
if (count%4==0) {
count=count*5/4+1;
isInt=true;
}else isInt=false;
}
if(isInt)
{System.out.print(count+"\t");
System.out.println(x);}
}
}
}
public class MonkeyPeach
{
public static int peachnum=6; //任意设置一个桃子的初始数量,用以递归。
public static int monkeynum=5; //猴子总数,可以任意设置猴子数量。
public static void main(String[] args)
{
peach(peachnum,monkeynum);
}
public static void peach(int num,int monkey)
{
if(monkey==0)
{
System.out.println("总桃子数为:"+num);
System.exit(0);
}
if(num%4!=0)
{
do
{
peachnum++;
}
while(peachnum%4!=0);
peach(peachnum,monkeynum);
}
peach(num/4*5+1,--monkey);
}
}
package fzu.test;
/**
* 一道笔试题目:
*海滩上有一堆桃子,五只猴子来分。第一只猴子把这堆桃子凭据分为五份,
*多了一个,这只猴子把多的一个扔入海中,拿走了一份。第二只猴子把剩
*下的桃子又平均分成五份,又多了一个,它同样把多的一个扔入海中,拿
*走了一份,第三、第四、第五只猴子都是这样做的,问海滩上原来最少有
*多少个桃子?
* @author yeruping
*
*/
public class Peaker {
/*
* 以下方法理解错误,题目的意思是一只猴子拿走5份中的一份时,剩下4份由下一只猴子再分
* 而下面的方法是其中的一份再分!理解错误
*/
public void count(){
int sum =0;//一共有几多
int[] per = new int[5];//每只猴子分的桃子数
per[4] = 1;//要求最少的桃子数,既最后一只猴子分到1只桃子
for(int i=3;i>=0;i--){
per[i]=per[i+1]*5+1;
}
sum = per[0]*5+1;
System.out.println("每只分别至少分到:"+per[0]+" "+per[1]+" "+per[2]+" "+per[3]+" "+per[4]+"桃子 至少总共:"+sum);
}
public void countRight(){
/**
* 直接思路开始算,都符合条件后跳出循环
* 借用 vampireallen 的 呵呵
* 不过取消了小于4000同样可以进行的
*/
int sum = 0;
int[] per = new int[5]; //每只猴子分的桃子数
boolean flag = false;
for (int i = 0; ; i++) {//从0开始向上增长,到后面flag真时跳出!
sum = i;
int j = 0;
for (; j < 5; j++) {
if ((sum - 1) % 5 != 0) {
break;
}
sum = (sum - 1) * 4 / 5;
per[j]=sum;
if (j == 4) {
flag = true;
}
}
if (flag == true) {
System.out.println("每只分别至少分到:"+per[0]+" "+per[1]+" "+per[2]+" "+per[3]+" "+per[4]+"桃子 至少总共:"+i);
break;
}
package interview;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int count=1;
for(int i=1;i<6;i++){
count=count*5+1;
}
System.out.println(count);
}
}
count:3906