Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 586|回复: 0

[算法学习]C语言趣味程序百例精解之JAVA实现(77)波瓦松分酒

[复制链接]
  • TA的每日心情
    开心
    2021-3-12 23:18
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2014-11-14 00:08:34 | 显示全部楼层 |阅读模式
    C语言趣味程序百例精解之java实现(77)波瓦松分酒趣题

    用递归实现了:
    1. import java.io.*;
    2. public class Test{
    3. int V[]={12,8,5};//瓶子容量
    4. int src[] ={0,0,1,1,2,2};//有6种倒酒方法,src[0]->dest[0]表示大瓶往中瓶倒,src[3]->dest[3]表示中瓶往小瓶倒,其它类同.
    5. int dest[]={1,2,0,2,0,1};
    6.   /* 记录分酒步骤的数组:如
    7.    * A     B    C
    8.    * 12    0    0
    9.    * 4     8    0
    10.    * 0     8    4
    11.    * 8     0    4
    12.    * 8     4    0
    13.    * 3     4    5
    14.    * 3     8    1
    15.    * 11    0    1
    16.    * 11    1    0
    17.    * 6     1    5
    18.    * 6     6    0
    19.    */
    20. int record[][]=new int[100][3];
    21. int rec_index=0;
    22. int count=1;//分酒方法的种数
    23. //在控制台和文件中输出
    24. public void Output(PrintWriter pw)
    25. {
    26.    pw.printf("(%d)----------------------------------
    27. ",count);
    28.    System.out.printf("(%d)----------------------------------
    29. ",count);
    30.     pw.printf("    A    B    C
    31. ");
    32.     System.out.printf("    A    B    C
    33. ");
    34.     for(int i=0;i< rec_index;++i){
    35.         System.out.printf("%4d %4d %4d
    36. ",record[i][0],record[i][1],record[i][2]);
    37.         pw.printf("%4d %4d %4d
    38. ",record[i][0],record[i][1],record[i][2]);
    39.      }
    40.    
    41.    
    42. }
    43. public void Record(int state[])//记录步骤
    44. {
    45.     record[rec_index][0]=state[0];
    46.     record[rec_index][1]=state[1];
    47.     record[rec_index][2]=state[2];
    48.     ++rec_index;
    49. }
    50. public boolean Exist(int state[])//是否回到了前一步
    51. {
    52.     for(int i=0;i< rec_index;++i)
    53.         if (state[0]==record[i][0]
    54.          && state[1]==record[i][1]
    55.          && state[2]==record[i][2])
    56.             return true;
    57.     return false;
    58. }
    59. public void Solve(int state[],PrintWriter pw) //递归求解
    60. {
    61.     int a=state[0],b=state[1],c=state[2];
    62.     Record(state);
    63.     if(a==6 && b==6 && c==0) { //找到一种分酒方法,输出
    64.        Output(pw);
    65.        count++;
    66.        return;
    67.     }
    68.     for(int i=0;i< 6;++i)//针对六种倒酒方法,递归求出每一种的所有解。
    69.     {
    70.         if(state[src[i]]==0) continue;//
    71.         Pour(state,src[i],dest[i]);
    72.         if(!Exist(state))
    73.         {
    74.             Solve(state,pw);
    75.             --rec_index;
    76.         }
    77.         state[0]=a;state[1]=b;state[2]=c;
    78.     }
    79. }
    80. public void Pour(int state[],int x,int y)
    81. {
    82.     int r=V[y]-state[y];//计算y瓶中还可以盛多少酒,y的总容量-y的状态(当时的容量)
    83.     if(state[x]< r) {state[y]+=state[x];state[x]=0;}//将x中的酒全部倒入y
    84.     else {state[y]=V[y];state[x]-=r;}//从x瓶中倒入r品脱酒到y瓶中,此时y瓶满
    85. }
    86.   public static void main(String args[]) throws IOException
    87. {
    88.     //PrintWriter pw=new PrintWriter(new OutputStreamWriter(new FileOutputStream("result.txt")),false);
    89.     File file=new File("result.txt");//将结果写入文件
    90.     PrintWriter pw=new PrintWriter(new FileWriter(file));
    91.     int init[]={12,0,0};
    92.     new Test().Solve(init,pw);
    93.     pw.close();
    94.     }
    95. }
    96. 运行结果:
    97. (1)----------------------------------
    98.     A    B    C
    99.   12    0    0
    100.    4    8    0
    101.    0    8    4
    102.    8    0    4
    103.    8    4    0
    104.    3    4    5
    105.    3    8    1
    106.   11    0    1
    107.   11    1    0
    108.    6    1    5
    109.    6    6    0
    110. (2)----------------------------------
    111.     A    B    C
    112.   12    0    0
    113.    4    8    0
    114.    4    3    5
    115.    0    7    5
    116.    7    0    5
    117.    7    5    0
    118.    2    5    5
    119.    2    8    2
    120.    0    8    4
    121.    8    0    4
    122.    8    4    0
    123.    3    4    5
    124.    3    8    1
    125.   11    0    1
    126.   11    1    0
    127.    6    1    5
    128.    6    6    0
    129. (3)----------------------------------
    130. 。。。。。。。。。。。。。。。。。。。。。。。
    131. (120)----------------------------------
    132.     A    B    C
    133.   12    0    0
    134.    7    0    5
    135.    7    5    0
    136.    2    5    5
    137.    2    8    2
    138.    4    8    0
    139.    4    3    5
    140.    9    3    0
    141.    9    0    3
    142.    1    8    3
    143.    1    6    5
    144.    0    7    5
    145.    0    8    4
    146.    8    0    4
    147.    8    4    0
    148.    3    4    5
    149.    3    8    1
    150.   11    0    1
    151.   11    1    0
    152.    6    1    5
    153.    6    6    0
    154. (121)----------------------------------
    155.     A    B    C
    156.   12    0    0
    157.    7    0    5
    158.    7    5    0
    159.    2    5    5
    160.    2    8    2
    161.    4    8    0
    162.    4    3    5
    163.    9    3    0
    164.    9    0    3
    165.    1    8    3
    166.    1    6    5
    167.    6    6    0
    复制代码
      
       
         
         
          
          

            
          

            
          
         
       

      


    源码下载:http://file.javaxxz.com/2014/11/14/000834312.zip
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2025-2-25 07:54 , Processed in 0.328243 second(s), 34 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表