Mother's Milk母亲的牛奶

 Mother's Milk母亲的牛奶
农民约翰有三个容量分别是 A,B,C 升的桶,A,B,C 分别是三个从 1 到 20 的整数,最初,A和B桶都是
空的,而 C 桶是装满牛奶的.有时,约翰把牛奶从一个桶倒到另一个桶中,直到被灌桶装满或原桶空
了.当然每一次灌注都是完全的.由于节约,牛奶不会有丢失.
写一个程序去帮助约翰找出当 A 桶是空的时候,C 桶中牛奶所剩量的所有可能性.
PROGRAM NAME: milk3
INPUT FORMAT
单独的一行包括三个整数 A,B 和 C.
SAMPLE INPUT (file milk3.in)
8 9 10
OUTPUT FORMAT
只有一行,列出当 A 桶是空的时候,C 桶牛奶所剩量的所有可能性.
SAMPLE OUTPUT (file milk3.out)
1 2 8 9 10
SAMPLE INPUT (file milk3.in) 
2 5 10
SAMPLE OUTPUT (file milk3.out)
5 6 7 8 9 10

=======================

反过来看一下,发现写了好多重复的代码..

实际上可以实际一个子程序..完成统一操作..

============

{
ID:jie19952
PROG:milk3
LANG:PASCAL
}
var
  a,b,c:longint;
  bool:array[0..20,0..20,0..20]of boolean;
  ans:array[0..1000]of longint;
procedure init;
begin
  assign(input,'milk3.in');
  assign(output,'milk3.out');
  reset(input); rewrite(output);
end;

procedure terminate;
begin
  close(input); close(output);
  halt;
end;

procedure dfs(a1,b1,c1:longint);
var
  c_a,c_b,c_c:longint;
begin
  if a1=0 then
    begin
      inc(ans[0]);
      ans[ans[0]]:=c1;
    end;
  c_a:=a-a1; //a中还可装多少?
  c_b:=b-b1;
  c_c:=c-c1;
  {1}{b->a}
  if b1>=c_a then
    begin
      if not bool[a,b1-c_a,c1] then
        begin
          bool[a,b1-c_a,c1]:=true;
          dfs(a,b1-c_a,c1);
        end;
    end
    else
    begin
      if not bool[a1+b1,0,c1] then
        begin
          bool[a1+b1,0,c1]:=true;
          dfs(a1+b1,0,c1);
        end;
    end;
  {2}{c->a}
  if c1>=c_a then
    begin
      if not bool[a,b1,c1-c_a] then
        begin
          bool[a,b1,c1-c_a]:=true;
          dfs(a,b1,c1-c_a);
        end;
    end
    else
    begin
      if not bool[a1+c1,b1,0] then
        begin
          bool[a1+c1,b1,0]:=true;
          dfs(a1+c1,b1,0);
        end;
    end;
  {3}{a->b}
  if a1>=c_b then
    begin
      if not bool[a1-c_b,b,c1] then
        begin
          bool[a1-c_b,b,c1]:=true;
          dfs(a1-c_b,b,c1);
        end;
    end
    else
    begin
      if not bool[0,b1+a1,c1] then
        begin
          bool[0,b1+a1,c1]:=true;
          dfs(0,b1+a1,c1);
        end;
    end;
  {4}{c->b}
  if c1>=c_b then
    begin
      if not bool[a1,b,c1-c_b] then
        begin
          bool[a1,b,c1-c_b]:=true;
          dfs(a1,b,c1-c_b);
        end;
    end
    else
    begin
      if not bool[a1,b1+c1,0] then
        begin
          bool[a1,b1+c1,0]:=true;
          dfs(a1,b1+c1,0);
        end;
    end;
  {5}{a->c}
  if a1>=c_c then
    begin
      if not bool[a1-c_c,b1,c] then
        begin
          bool[a1-c_c,b1,c]:=true;
          dfs(a1-c_c,b1,c);
        end;
    end
    else
    begin
      if not bool[0,b1,c1+a1] then
        begin
          bool[0,b1,c1+a1]:=true;
          dfs(0,b1,c1+a1);
        end;
    end;
  {6}{b->c}
  if b1>=c_c then
    begin
      if not bool[a1,b1-c_c,c] then
        begin
          bool[a1,b1-c_c,c]:=true;
          dfs(a1,b1-c_c,c);
        end;
    end
    else
    begin
      if not bool[a1,0,c1+b1] then
        begin
          bool[a1,0,c1+b1]:=true;
          dfs(a1,0,c1+b1);
        end;
    end;
end;

procedure main;
var
  i,j:longint;
  tem:longint;
begin
  readln(a,b,c);
  fillchar(bool,sizeof(bool),false);
  ans[0]:=0;

  bool[0,0,c]:=true;
  dfs(0,0,c);
  for i:=1 to ans[0]-1 do
    for j:=i+1 to ans[0] do
      if ans[i]>ans[j] then
      begin
        tem:=ans[i];
        ans[i]:=ans[j];
        ans[j]:=tem;
      end;
  for i:=1 to ans[0]-1 do
    write(ans[i],' ');
  writeln(ans[ans[0]]);
end;

begin
  init;
  main;
  terminate;
end.


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值