泊松是法国数学家、物理学家和力学家。他一生致力科学事业,成果颇多。有许多著名的公式定理以他的名字命名,比如概率论中著名的泊松分布。
有一次闲暇时,他提出过一个有趣的问题,后称为:“泊松分酒”。在我国古代也提出过类似问题,遗憾的是没有进行彻底探索,其中流传较多是:“韩信走马分油”问题。
有3个容器,容量分别为12升,8升,5升。其中12升中装满油,另外两个空着。要求你只用3个容器操作,最后使得某个容器中正好有6升油。
下面的列表是可能的操作状态记录:
12,0,0
4,8,0
4,3,5
9,3,0
9,0,3
1,8,3
1,6,5
每行3个数据,分别表示12,8,6升容器中的油量
第一行表示初始状态,第二行表示把12升倒入8升容器后的状态,第三行是8升倒入5升,...
当然,同一个题目可能有多种不同的正确操作步骤。
本题目的要求是,请你编写程序,由用户输入:各个容器的容量,开始的状态,和要求的目标油量,程序则通过计算输出一种实现的步骤(不需要找到所有可能的方法)。如果没有可能实现,则输出:“不可能”。
例如,用户输入:
12,8,5,12,0,0,6
用户输入的前三个数是容器容量(由大到小),接下来三个数是三个容器开始时的油量配置,最后一个数是要求得到的油量(放在哪个容器里得到都可以)
则程序可以输出(答案不唯一,只验证操作可行性):
12,0,0
4,8,0
4,3,5
9,3,0
9,0,3
1,8,3
1,6,5
每一行表示一个操作过程中的油量状态。
注意:
请仔细调试!您的程序只有能运行出正确结果的时候才有机会得分!
请把所有类写在同一个文件中,调试好后,存入与【考生文件夹】下对应题号的“解答.txt”中即可。
相关的工程文件不要拷入。
请不要使用package语句。
源程序中只能出现JDK1.5中允许的语法或调用。不能使用1.6或更高版本。
import java.util.ArrayList;
import java.util.Scanner;
public class FeiJiu {
static ArrayList<Jiutong> list = new ArrayList<Jiutong>();//保存记录
static ArrayList<String> remove = new ArrayList<String>();//用来避免重复的
static int[] dest =new int [3];//记录个个酒桶的容量大小
static int re=6;//目标容量
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
System.out.println("用户输入:");
String userStr=scan.nextLine();
String user []=userStr.split(",");
int[] arr1 =new int[3];
re=Integer.parseInt(user[user.length-1]);
dest[0]=Integer.parseInt(user[0]);
dest[1]=Integer.parseInt(user[1]);
dest[2]=Integer.parseInt(user[2]);
arr1[0]=Integer.parseInt(user[3]);
arr1[1]=Integer.parseInt(user[4]);
arr1[2]=Integer.parseInt(user[5]);
new FeiJiu().find(arr1);
}
public void find(int arr[]) {
String str = arr[0] + "," + arr[1] + "," + arr[2] + "->";
Jiutong jt = new Jiutong(arr, str);
list.add(jt);// 入队
remove.add(str);
while (list.size() > 0) {
Jiutong tempJs = list.get(0);// 出队
int[] tempA = new int[3];
System.arraycopy(tempJs.arr, 0, tempA, 0, tempA.length);
String bz = tempJs.arr[0] + "," + tempJs.arr[1] + ","
+ tempJs.arr[2] + "->";
//判断是否符合条件
if (tempJs.arr[0] == 6 || tempJs.arr[1] == 6 || tempJs.arr[2] == 6) {
System.out.println(tempJs.bz);
return;
}
list.remove(0);
//A->B
if (tempJs.arr[0] != 0 && tempJs.arr[1] != dest[1]) {
int[] tempArr = daoJiu(tempA, 0, 1);
add(tempArr,tempJs);
System.arraycopy(tempJs.arr, 0, tempA, 0, tempA.length);
}//A->C
if (tempJs.arr[0] != 0 && tempJs.arr[2] != dest[2]) {
int[] tempArr = daoJiu(tempA, 0, 2);
add(tempArr,tempJs);
System.arraycopy(tempJs.arr, 0, tempA, 0, tempA.length);
}//B->A
if (tempJs.arr[1] != 0 && tempJs.arr[0] != dest[0]) {
int[] tempArr = daoJiu(tempA, 1,0);
add(tempArr,tempJs);
System.arraycopy(tempJs.arr, 0, tempA, 0, tempA.length);
}//B->C
if (tempJs.arr[1] != 0 && tempJs.arr[2] != dest[2]) {
int[] tempArr = daoJiu(tempA, 1, 2);
add(tempArr,tempJs);
System.arraycopy(tempJs.arr, 0, tempA, 0, tempA.length);
}//C->A
if (tempJs.arr[2] != 0 && tempJs.arr[0] != dest[0]) {
int[] tempArr = daoJiu(tempA,2,0);
add(tempArr,tempJs);
System.arraycopy(tempJs.arr, 0, tempA, 0, tempA.length);
}//C->B
if (tempJs.arr[2] != 0 && tempJs.arr[1] != dest[2]) {
int[] tempArr = daoJiu(tempA, 2, 1);
add(tempArr,tempJs);
System.arraycopy(tempJs.arr, 0, tempA, 0, tempA.length);
}
}
}
//添加操作
public void add(int tempArr[],Jiutong tempJs)
{
String tempStr = tempArr[0] + "," + tempArr[1] + ","
+ tempArr[2] + "->";
if (!remove.contains(tempStr)) {
Jiutong tempJt = new Jiutong(tempArr, tempJs.bz + tempStr);
list.add(tempJt);// 入队
remove.add(tempStr);
}
}
//倒酒操作
public static int[] daoJiu(int[] arrT, int x, int y) {
int arr[] = new int[3];
System.arraycopy(arrT, 0, arr, 0, arr.length);
int num = dest[y] - arr[y];
if (arr[x] - num >= 0) {
arr[x] = arr[x] - num;
arr[y] += num;
} else {
arr[y] += arr[x];
arr[x] = 0;
}
return arr;
}
//模拟三个酒桶
class Jiutong {
int[] arr;//三个酒桶的状态
String bz;//记录步骤
Jiutong(int[] arr, String bz) {
this.arr = arr;
this.bz = bz;
}
public int[] getArr() {
return arr;
}
public void setArr(int[] arr) {
this.arr = arr;
}
public String getBz() {
return bz;
}
public void setBz(String bz) {
this.bz = bz;
}
}
}