Topic: Mind twister

  Print this page

1.Mind twister Copy to clipboard
Posted by: floater
Posted on: 2004-04-26 08:02

小明要背3000个包子到MM家, 小明每次最多只能背1000个包子, 而小明每走一公里要吃一个包子, 从出发到MM家的距离是1000公里. 小明最多可以把多少个包子带给MM?

from epubcn

2.Re:Mind twister [Re: floater] Copy to clipboard
Posted by: jigsaw
Posted on: 2004-04-26 09:12

小明带999个包子出发 走333公里 然后回来 路上可以每隔1公里放一个包子
然后再去的时候带上1000个包子作路费 这样至少可以带333个包子去见mm
答案应该比333大吧?
不知道这个思路对不对?

3.Re:Mind twister [Re: floater] Copy to clipboard
Posted by: floater
Posted on: 2004-04-27 00:37

I don't know, just saw that one and posted here. Don't have time to think about it yet. Sounds right.

4.Re:Mind twister [Re: floater] Copy to clipboard
Posted by: jigsaw
Posted on: 2004-04-27 10:48

hi flo, if there's an answer posted, pls paste it here.. thx~

5.Re:Mind twister [Re: floater] Copy to clipboard
Posted by: bwpc
Posted on: 2004-04-27 10:50

我是这样算的
假设一次的最大功效数为w,剩下的包子数为x,前进的距离为y,那么
求w=x*y的最大值就是我们希望最优的

1.起点每次满载应该最优
w=(1000-2y)*y y取值[0,500],求导得出y=250公里

在250公里处的状态为:剩下1750个包子
2. 250公里处,同样第一次满载走250公里回来取剩下的750个包子

在500公里处的状态为:剩下1000个包子
此后满载1000个包子到达终点,可以剩下500个给mm

以上我个人的理解,呵呵

6.Re:Mind twister [Re: floater] Copy to clipboard
Posted by: floater
Posted on: 2004-04-27 21:39

I don't have answers.

假设一次的最大功效数为w,剩下的包子数为x,前进的距离为y,那么
求w=x*y的最大值就是我们希望最优的
My guts feeling is this too, but we need to prove this because the final result is maxFight not max(xy).
1.起点每次满载应该最优

w is max when 1000-2y=y --> y=333

The rest is not correct:
在250公里处的状态为:剩下1750个包子...

because you can carry 1000 max at once, you have to make round trip several times to move all of them.

don't have time to think about others.

7.Re:Mind twister [Re: floater] Copy to clipboard
Posted by: bwpc
Posted on: 2004-04-27 23:49

w=(1000-2y)*y 当y在【0,500】区间取值,是y=250的时候,这个楼主可以在算一次,求阶导数,令其为零即可,呵呵

其实这个消耗主要是在路程上,当然如果小明负载越大,那么最后剩下的包子就越多,当负载一定,如果多回来一趟 往前运的数量大于来回一趟消耗的,那么还是值得回来一趟的

0-250区间
运3趟
第一次满载1000,到达250公里处,剩下750个,放下500个,带回250个供返回路程吃
第二趟同样运过去500个
第三趟由于不需要返回,所以剩下750个

所以说在250公里处有1750个包子
接下来可以把250公里处做为起点,包子数量是1750,继续算下去

要判断的是:是否值得回来取剩下的包子

ps:我给不出很严格的数学证明,呵呵。

8.Re:Mind twister [Re: floater] Copy to clipboard
Posted by: floater
Posted on: 2004-04-28 01:21

Oh, I misunderstood you on step 1, hehe...
sounds right.

9.Re:Mind twister [Re: floater] Copy to clipboard
Posted by: floater
Posted on: 2004-05-11 04:32


/**
* There are M breads at the starting point, the distance is N miles from the
* beginning to the end. A person can carry C breads and will consume 1 per mile.
* How many breads will be eventually carried over to the end?
*
*/

public class Moving
{
private int m, n, c;

public Moving(int M, int N, int C)
{
m = M;
n = N;
c = C;
}

public int move()
{
int total = m;
for (int i=0; i<n; i++)
{
System.out.println("moving 1 mile ..." + (i+1));
total = move(total, 1);
// This test is to show: (c-2*miles)*miles has a maximum
// moving a distance up to this maximal point, this cost is the
// same as moving 1 by 1 mile.
System.out.println("test this: " + (c-2*(i+1))*(i+1));
}
return total;
}

private int move(int totalBreads, int milesMoved)
{
System.out.println("total: " + totalBreads);
if (totalBreads <= 0 || milesMoved < 0) return 0;
// can't carry all of them once, so need to make a round trip.
if (totalBreads > c)
{
if (totalBreads - c > 2*milesMoved) // worth to come back
{
//System.out.println("going back ...");
return c - 2 * milesMoved + move(totalBreads - c, milesMoved);
}
else // not worth to come back, so disgard the rest.
{
//System.out.println("not going back ...");
return c - milesMoved;
}
}
// can carry them once.
else if (totalBreads <= milesMoved) return 0;
else return totalBreads - milesMoved;
}

public static void main(String[] args)
{
Moving moving = new Moving(3000, 1000, 1000);
System.out.println("total moved: " + moving.move());
}
}

10.Re:Mind twister [Re: floater] Copy to clipboard
Posted by: bwpc
Posted on: 2004-05-11 10:01

excellent
我觉得这个答案是对的
对于全程N来说,运到N/4是最优的,那么对于N/4距离来说,运到N/16 是最优的,依次类推并且取最小整数就是1了,呵呵,所以楼主的运算应该是对的,呵呵

程序中这个条件 if (totalBreads - c > milesMoved) // worth to come back
应该改成 if (totalBreads - c > 2*milesMoved),当然这并没有影响最后的结果(由于这个milesMoved取值为1)

11.Re:Mind twister [Re: floater] Copy to clipboard
Posted by: pengtaoli
Posted on: 2004-05-11 10:41

每次都带上最多包子1000。

第一次走应该是333。
3*X=1000
333公里都会留下包子。

第二次走应该是444
3*(333+X)=333+1000
444公里都会留下包子。

第三次到MM家剩下444个。

但是有两个没带上,可能不是最优解,不过应该是一个接近最优的解了。

12.Re:Mind twister [Re: floater] Copy to clipboard
Posted by: bwpc
Posted on: 2004-05-11 20:15

补充一下:
设置迭代步长卫d=N/(4**n),当d越小,最后的结果越大,d->0时就可以取得极限值,我把n取20,d约9exp-10,经过大约10exp12次循环迭代结果非常接近533+1/3
步长取1,和1/2会得到相同的结果:533
由于包子不可分,因此我觉得floater用步长为1的求解应该是正确的答案

btw:哪个学数学的,可以用数学知识求一下这个极限值,验证一下是不是533+1/3

13.Re:Mind twister [Re: floater] Copy to clipboard
Posted by: floater
Posted on: 2004-05-11 21:56

I turned on the prints and noticed:
when number of 包子 is between 1k-2k, since anything above 1k needs to make an extra round trip, the maximal of miles moved in 1 trip is 1/3 of the number of 包子 >1k. Similarly, between 2k-3k, it's 1/5(2 round trips + 1 one way), and so on. So the maximal miles in 1 trip is varying, depends on the number of 包子, not fixed at 1/4.

14.Re:Mind twister [Re: bwpc] Copy to clipboard
Posted by: floater
Posted on: 2004-05-11 22:10

bwpc wrote:

程序中这个条件 if (totalBreads - c > milesMoved) // worth to come back
应该改成 if (totalBreads - c > 2*milesMoved),当然这并没有影响最后的结果(由于这个milesMoved取值为1)

good catch!!!

15.Re:Mind twister [Re: floater] Copy to clipboard
Posted by: bwpc
Posted on: 2004-05-12 02:07

you are right.
我想了一下,四分之一点是不存在的,另外最大功效数的提法也是错误的,呵呵

先不推广到一般情况,就这个题目而言,当包子在3k——2k之间的时候需要返回两次,设需要返回2次的距离为x,那么损失的包子就是5x
当包子数在1k-2k之间,那么需要返回一次,设这段距离为y,那么损失的包子就是3y
不需要返回的距离为z,损失包子数为z
存在如下关系 x+y+z=1000
并做如下假设:如果包子数不大于2k,那么我们绝不希望返回两次
如果包子数不大与1k,那么我们绝不希望返回1次

包子从3k消耗到2k的距离(即x的值) x=1000/5=200
包子从2k消耗到1k的距离(即y的值) y=1000/3=333(取整)
z=1000-x-y=467

也就是说把整个距离分成3个不同的区间
[0,200] [200,533] [533,100] 分别为返回两次,一次,不需要返回

在每个区间里,随便选择运送方式(即步长任意,保证每个步长返回的次数和对应区间的一致)结果是一样的
最后的结果当然是533了,如果考虑包子可以分,那么就是533+1/3

当然也可以推广到一般情况,即包子数,运送能力,运送距离是变量的情况

16.Re:Mind twister [Re: floater] Copy to clipboard
Posted by: floater
Posted on: 2004-05-12 09:30

good summary


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923