求助各位高人:面试题目:一个桶中有12斤油,要求倒出6斤,可现在另外只有两个桶,分别可装8斤与5斤,请问应如何来倒?(急啊)

swallowjack 2009-02-24 11:06:20
加精
求助各位高人:分油算法:一个桶中有12斤油,要求倒出6斤,可现在另外只有两个桶,分别可装8斤与5斤,请问应如何来倒?
补充:这里的12,6,8,5都是变量,应该可以自己设置,输出是每一次分油的步骤。
...全文
10735 293 打赏 收藏 转发到动态 举报
写回复
用AI写文章
293 条回复
切换为时间正序
请发表友善的回复…
发表回复
xuesh01 2010-09-12
  • 打赏
  • 举报
回复
菜鸟飘过,现在对我还太难了~
mirrror1986 2010-08-26
  • 打赏
  • 举报
回复
BFS+剪枝
songfei5201314 2010-05-02
  • 打赏
  • 举报
回复
有项目管理经验的.NET开发的朋友,可以加上限500人的QQ群28720769,一起交流。
灵松 2010-04-07
  • 打赏
  • 举报
回复
先藏了,以后慢慢学了。
qinheng 2010-04-06
  • 打赏
  • 举报
回复
第一题答案:

12的桶 5的桶 8的桶
12 0 0
4 0 8
4 5 3
9 0 3
9 3 0
1 3 8
1 5 6

用程序写代码如下:
public class GetTPath
{
private bool iswhile = true;
private string checkedstr = ";";
public string GetPath(int[] nowarray, int t1, int t2, int t3, int remain)
{
string laststr = ToStr(nowarray) + ";";
checkedstr += laststr;
string nowpath = "";
if (nowarray[0] == remain)
{
iswhile = false;
}
else
{
string cns = ",";
while (iswhile)
{
nowpath = "";
//取最大值
int mn = 0;
int mi = 0;
for (int i = 0; i < 3; i++)
{
if (nowarray > mn && cns.IndexOf("," + nowarray.ToString() + ",") < 0)
{
mn = nowarray;
mi = i;
cns += mn.ToString() + ",";
}
}
if (mn == 0)
{
break;
}
//倒
for (int i = 0; i < 3; i++)
{
if (i != mi)
{
int tn = nowarray;
int t = 0;
if (i == 0)
{
t = t1;
}
else if (i == 1)
{
t = t2;
}
else if (i == 2)
{
t = t3;
}
int[] newinta = new int[3];
nowarray.CopyTo(newinta, 0);
newinta = ((tn + mn) > t) ? t : tn + mn;
newinta[mi] = ((tn + mn) > t) ? tn + mn - t : 0;
if (!IsExits(newinta))
{
//下一步
nowpath += GetPath(newinta, t1, t2, t3, remain);
break;
}
}
}
}
}
return laststr + nowpath;
}
private string ToStr(int[] array)
{
string str = "";
for (int i = 0; i < array.Length; i++)
{
str += array.ToString() + ",";
}
return str;
}
private bool IsExits(int[] array)
{
if (checkedstr.IndexOf(";" + ToStr(array)) < 0)
{
return false;
}
else
{
return true;
}
}
}

protected void Page_Load(object sender, EventArgs e)
{
int t1 = 12;
int t2 = 8;
int t3 = 5;
int remain = 6;
int[] bints = new int[] { 12, 0, 0 };
GetTPath gtp = new GetTPath();
string str = gtp.GetPath(bints, t1, t2, t3, remain);
Response.Write(str.Replace(";","<br>"));
}

//输出:
12,0,0,
4,8,0,
4,3,5,
9,3,0,
9,0,3,
1,8,3,
1,6,5,
6,6,0,
sk_sakula 2010-03-16
  • 打赏
  • 举报
回复
已经把算法给你写出来了啊,你自己写一下是现在而已
chhch11111 2010-03-16
  • 打赏
  • 举报
回复
[Quote=引用 68 楼 bat603 的回复:]
经验证,必须符合以下条件才能倒出:(桶12为A 桶8为B 桶5为C, 倒出6为D )
1. B+C>A
2. B <A, C <A
3. C <D <B或B <D <C
具体算法,还请算法大师给出
[/Quote]
A再多都能倒得出,A太少才没办法倒,A给你个8看你倒不倒得出来,不知道你怎么验证的
chhch11111 2010-03-16
  • 打赏
  • 举报
回复
[Quote=引用 54 楼 justxd 的回复:]
static void Main(string[] args)
{
int A = 12;
int B = 0;//B不能大于8
int C = 0;//C不能大于5

//第一次用A将B倒满
B=8;
A-=B;

……
[/Quote]
你用8,4算看看,算的出来算你牛
ljia0 2010-03-11
  • 打赏
  • 举报
回复
[Quote=引用 129 楼 zcs198404 的回复:]
#include <stdio.h>
int i;
main()
{  int a,y,z;
    printf("Input Full a ,Empty b,c,Get i:");  /*读入3个容器的容量和最后需要的数量*/
    scanf("%d,%d,%d,%d",&a,&y,&z,&i);
    getti(a,y,z);
}
getti(int a,int y,int z)
{  int b=0,c=0;  /*b,c为二个容器的实际重量*/
    printf("a%4d,b%4d,c%4d\n%4d,%4d,%4d\n",a,y,z,a,b,c);

    while(a!=i||b!=i)      /*如果满足要求退出循环*/
    {  if(!b)          /*如果b为空,从a往b倒油*/
        {  a-=y;b=y;
        }
        else if(c==z)
        {  a+=z;c=0;    /*如果c已满,从c往a倒油*/
        }
        else if(b>z-c)
        {  b-=(z-c);c=z;  /*如果b的重量大于c的剩余重量,倒满c*/
        }
        else
        {  c+=b;b=0;  /*否则将b中的油全部倒入c*/
        }
        printf("%4d%4d%4d\n",a,b,c);
    }
    getch();
}
[/Quote]
你这个还是不完善,当输入为:18,8,3,6的时候就是死循环。
xnagameboy 2010-03-11
  • 打赏
  • 举报
回复
这个牛.
jiney555 2010-03-04
  • 打赏
  • 举报
回复
1、倒满5斤桶,
2、把5斤桶油倒入8斤桶,
3、八斤桶里的5斤油倒出1斤,(留下8斤桶的一半)
4、再从12斤桶里倒出和5斤桶里一样多的油
即5+1=6(斤)
qiuzhenguang 2010-02-25
  • 打赏
  • 举报
回复
好贴!好贴!好贴!好贴!好贴!
金融砖家 2010-01-27
  • 打赏
  • 举报
回复
hha
pingan300 2009-12-17
  • 打赏
  • 举报
回复
好帖子,非常值得学习
starc 2009-05-13
  • 打赏
  • 举报
回复
不就是个算24的游戏 吗
knowcraft 2009-04-24
  • 打赏
  • 举报
回复
设桶12为a,8为b,5为c
a->b 8,
b->c 5,
c->a 5,
b->c 3,
a->b 8,
b->c 2,
b中还有6,外面也是6,解决
maosher 2009-04-21
  • 打赏
  • 举报
回复
12 0 0
8 4 0
3 4 5
3 8 1
11 0 1
11 1 0
6 1 5
6 6 0
maosher 2009-04-21
  • 打赏
  • 举报
回复

条件是每次操作后一至少有一个节点为满或者为空

12 0 0
8 4 0
3 5 5
3 8 1
11 0 1
11 1 0
6 1 5
6 6 0
geek0 2009-04-14
  • 打赏
  • 举报
回复
开始
12 8 5
0 7 5
0 8 4
3 4 5
3 8 1
11 0 1
11 1 0
6 1 5
madshime 2009-04-14
  • 打赏
  • 举报
回复
思路比较简单,但是用代码实现可能比较复杂
加载更多回复(270)

33,010

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧