一道笔试题目:
海滩上有一堆桃子,五只猴子来分。第一只猴子把这堆桃子凭据分为五份,多了一个,这只猴子把多的一个扔入海中,拿走了一份。第二只猴子把剩下的桃子又平均分成五份,又多了一个,它同样把多的一个扔入海中,拿走了一份,第三、第四、第五只猴子都是这样做的,问海滩上原来最少有多少个桃子?
海滩上有一堆桃子,五只猴子来分。第一只猴子把这堆桃子凭据分为五份,多了一个,这只猴子把多的一个扔入海中,拿走了一份。第二只猴子把剩下的桃子又平均分成五份,又多了一个,它同样把多的一个扔入海中,拿走了一份,第三、第四、第五只猴子都是这样做的,问海滩上原来最少有多少个桃子?
解决方案 »
- StringBuffer 的引用传递问题?
- 有关用Hiberate实现增加商品的问题
- java链接SqlServer2000数据库的链接文怎么写啊。高人指教!
- 再发:用java的applet调用delphi封装第三方动态库函数的exe文件出错,急!!!!!分不够再加(在线等待)
- 关于四舍五入
- 读文件的两个小题?
- 这个怎么做?
- cannot make a static reference to the non-static field seg
- 如何生成8位的随机码?
- 谁有ANT的中文资料
- System.out.println(new Date() );疑问
- 我是新学JAVA的 请问下JDK帮助文档中的符号代表什么?
{ /**
* @param args
*/
public static void main(String[] args)
{
int n = 0; //最后一个猴子分到的桃子
int sum = 0; //总桃子数
int a[] = new int[5]; //分别存放五个猴子拿到的桃子数
boolean tag = false;
for (int i = 1; i < 5000; i++)
{
n = i;
for (int j = 0; j < 4; j++)
{ if ((n * 5 + 1) % 4 != 0)
{
break;
} n = (n * 5 + 1) / 4;
if (j == 3)
{
tag = true;
}
}
if (tag == true)
{
n = i;
a[4] = n;
break;
} } for (int k = 3; k >= 0; k--)
{ n = (n * 5 + 1) / 4;
a[k] = n; }
sum = 5 * n + 1;
System.out.println("一共有" + sum + "个桃子");
for (int i = 0; i < 5; i++)
{
System.out.println("第" + (i + 1) + "只猴子分到" + a[i] + "个桃子");
} }
}
public class Peaker { 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 static void main(String[] args) {
new Peaker().count();
}
}
public static void main(String[] args) {
int peaches = 0;
boolean hit = false;
for (int i = 0; i < 4000; i++) {
peaches = i;
for (int j = 0; j < 5; j++) {
if ((peaches - 1) % 5 != 0) {
break;
}
peaches = (peaches - 1) * 4 / 5;
if (j == 4) {
hit = true;
}
}
if (hit == true) {
System.out.println("peaches = " + i);
break;
} } }
int remainder = 0;
boolean hit = false;
for (int i = 0; i < 4000; i++) {
remainder = i;
for (int j = 0; j < 5; j++) {
if (remainder % 4 != 0) {
break;
}
remainder = remainder * 5 / 4 + 1;
if (j == 4) {
hit = true;
}
}
if (hit == true) {
System.out.println("peaches = " + remainder);
break;
} } }
{
//最少的时候当然是第五个猴子只取到一个的时候
int count=1;
for(int i=1;i<5;i++)
{
count=count*5+1;
System.out.println(count);
}
System.out.println("all the pench is :"+count);
}
31
156
781
all the pench is :781
public void countMonkeySharePench()
{
//最少的时候当然是第五个猴子只取到一个的时候
int count=1;
for(int i=1;i<6;i++)
{
count=count*5+1;
System.out.println(count);
}
System.out.println("all the pench is :"+count);
}
public void countMonkeySharePeach( )
{
int i=0, m, j=0, k, count;
for ( i = 4; i < 10000; i += 4 )
{
count = 0;
m = i;
for ( k = 0; k < 5; k++ )
{
j = i / 4 * 5 + 1;
i = j;
if ( j % 4 == 0 )
{
count++;
//System.out.println(j);
}
else
break;
}
i = m;
if ( count == 4 )
{
System.out.println (j);
break;
}
}
}
class Monkey{
public static void main(String[] args){
int count = 6;
while (count++){
boolean flag = true;
int temp = count;
for(int j=1;j<=5;j++){ if( (temp -1 ) % 5 == 0){
temp = ((temp - 1)/5) * 4;
}
else{
flag = false;
break;
}
}
if(flag) {
System.out.println(count);
break;
}
}
}
}
class Monkey{
public static void main(String[] args){
int count = 6;
while (count++){ boolean flag = true;
int temp = count;
for(int j=1;j <=5;j++){ if( (temp -1 ) % 5 == 0){
temp = ((temp - 1)/5) * 4;
}
else{
flag = false;
break;
}
}
if(flag) {
System.out.println(count);
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
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;
}
借用 vampireallen 的
顺便解释我6楼那个错误;
{
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);
}
}
* 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);}
}
}}
(3901-1)/5=780
(780*4-1)/5=??????虽然是java初学,但是这么明显的结果,中学生的水平也能看出来啊,不负责任的回帖
static int ok()
{
for(int i=1000;;i++)
{
int temp=i;
for(int j=0;j<5;j++)
if((i-1)%5==0)
{
i=(i-1)/5;
if(j==4)
return temp;
}
i=temp;
}
}
从1000开始就是为了避免出现781的结果,其实我觉得26楼的朋友的算法是最优化的,他假设第五个猴子的时候剩下6个桃子,这是最好的方法了,我的事枚举,有点耗时。
static int ok()
{
for(int i=0;;i++)
{
int temp=i;
for(int j=0;j<5;j++)
if((i-1)%5==0)
{
i=(i-1)/5*4;
if(j==4)
return temp;
}
i=temp;
}
}
哈哈哈,结果是3121,反正不管怎么说,我的代码是足够简单的,哈哈。