李白饮酒问题

hungon 2010-04-06 10:57:23
李白无事街上走,提壶去买酒。遇店加一倍,见花喝一斗。五遇花和店,喝光壶中酒。试问李白壶中原有多少斗酒?
使用for循环结构编程实现



敬请高手指点!感激不尽!
...全文
751 43 打赏 收藏 转发到动态 举报
写回复
43 条回复
切换为时间正序
请发表友善的回复…
发表回复
hungon 2010-04-13
  • 打赏
  • 举报
回复
多谢各位仁兄!
l_wenb 2010-04-09
  • 打赏
  • 举报
回复
关注此贴!
折腾的生活 2010-04-09
  • 打赏
  • 举报
回复
李白真是好酒量,肯定做过品酒员啊!要不就是窑子去多了!
simonloveailsa 2010-04-09
  • 打赏
  • 举报
回复
1: 0.15625
2: 0.1875
3: 0.21875
4: 0.25
5: 0.28125
6: 0.3125
7: 0.34375
8: 0.375
9: 0.40625
10: 0.4375
11: 0.46875
12: 0.53125
13: 0.46875
14: 0.5
15: 0.5625
16: 0.59375
17: 0.65625
18: 0.625
19: 0.6875
20: 0.71875
21: 0.78125
22: 0.8125
23: 0.84375
24: 0.90625
25: 1.03125
26: 0.71875
27: 0.75
28: 0.875
29: 0.9375
30: 0.96875
31: 1.0625
32: 1.09375
33: 1.15625
34: 1.28125
35: 1.125
36: 1.1875
37: 1.21875
38: 1.3125
39: 1.34375
40: 1.40625
41: 1.53125
42: 1.5625
43: 1.59375
44: 1.65625
45: 1.78125
46: 2.03125
47: 1.21875
48: 1.25
49: 1.375
50: 1.4375
51: 1.46875
52: 1.625
53: 1.6875
54: 1.71875
55: 1.8125
56: 1.84375
57: 1.90625
58: 2.0625
59: 2.09375
60: 2.15625
61: 2.28125
62: 2.53125
63: 2.125
64: 2.1875
65: 2.21875
66: 2.3125
67: 2.34375
68: 2.40625
69: 2.5625
70: 2.59375
71: 2.65625
72: 2.78125
73: 3.03125
74: 3.0625
75: 3.09375
76: 3.15625
77: 3.28125
78: 3.53125
79: 4.03125
simonloveailsa 2010-04-09
  • 打赏
  • 举报
回复
不过我发现输出的结果有重复,所以斗胆把27楼的程序改了一下:

/**
* “五遇花和店”,存在歧义,到底处理了5次还是10次? 我跟着楼上各位的意思,按10次走吧!
* 一共5处花,那么李酒仙一共喝完5斗酒(唐代1斗为2000mL,那就是10L,天啊!!!) !
* 最后喝光了酒,说明最后一次是遇到花,那么只需要穷举4花5店的组合;
* 题目既然指明要用for循环来解题,那么必定是外面一套迭代所有的可能
* 里面一套计算该可能的结果:
*/
public static void main(String args[]){
ArrayList<Double> lists = new ArrayList<Double>();//在这里多创建了一个数组
int ccnt=1;
for(int i=0xF;i<=0x1E0;i++){
if(Integer.bitCount(i)==4){
BigInteger bi=BigInteger.valueOf(i);
double n=1;
for(int j=0;j<9;j++){
if(bi.testBit(j))n++;
else n/=2;
}
lists.add(n);//将值添加到数组
System.out.println(ccnt+++":"+n);
}

}

for(int i = 0 ;i < lists.size();i++){//for循环遍历数组去除重复。
double mun = lists.get(i);

for(int j = i+1;j < lists.size();j++){
if(lists.get(j)==mun){
lists.remove(j);
}
}
}
int l = 1;
Iterator<Double> ite = lists.iterator();
while(ite.hasNext()){
System.out.println(l+++": "+ite.next());//输出
}
}

lwhhubei 2010-04-09
  • 打赏
  • 举报
回复
惊叹!围观!
justlearn 2010-04-08
  • 打赏
  • 举报
回复
简化了一下,最后用正序方式打印结果

public static void main(String[] args) {
/*
* 采用倒推法
* 假设0代表店,1代表花 由于最后一次肯定是花,对前9次进行排列组合
*/
int min = 0xF;// 000001111
int max = 0x1E0;// 111100000

//只为最后显示结果用
Map<String[], Double[]> map = new HashMap<String[], Double[]>();

//通过正则确定数据有效,有且只有4个1的二进制数为有效
Pattern p = Pattern.compile("[0]*1[0]*1[0]*1[0]*1[0]*");
// 排列出000001111~111100000之间所有可能
for (int i = min; i <= max; i++) {
//最后一次确定为花,所以固定为1
double alcohol = 1;
Double[] path = new Double[10];
path[0] = alcohol;

//转化成二进制格式
String str = Integer.toBinaryString(i);
//判断有效性
Matcher m = p.matcher(str);
if (m.matches()) {
// 小于9位,前面补空格
str = String.format("%9s", str);

String[] strArr = str.split("");
//最后一次确定为花
strArr[0] = "1";
//循环从1开始,跳过最后一次,也就是最后喝的那次
for (int j = 1; j < strArr.length; j++) {
if ("1".equals(strArr[j])) {
++alcohol;
} else {
alcohol = alcohol / 2;
}
path[j] = alcohol;
}
map.put(strArr, path);
}
}

//此处皆为打印结果,与逻辑无关
Set<Entry<String[], Double[]>> rSet = map.entrySet();
int cnt = 0;
for (Entry<String[], Double[]> entry : rSet) {
System.out.printf("%3s", ++cnt);
String[] strKey = entry.getKey();
Double[] dValue = entry.getValue();
for (int i = strKey.length-1; i >=0 ; i--) {
if ("1".equals(strKey[i])) {
System.out.print(" [花:");
System.out.printf("%-7s",dValue[i]);
System.out.print("] (-1)-> ");
} else {
System.out.print(" [店:");
System.out.printf("%-7s",dValue[i]);
System.out.print("] (*2)-> ");
}
}
System.out.println(" [0]");
}
}
simonloveailsa 2010-04-08
  • 打赏
  • 举报
回复
27楼,聪明。
yuyeyi 2010-04-08
  • 打赏
  • 举报
回复
感觉是全排列加反推
justlearn 2010-04-08
  • 打赏
  • 举报
回复
既然是说随机的遇店和花,我重新写了个,排列组合126种,不知道对不对。
可以自己运行下,结果太长,不贴了


public static void main(String[] args) {
/*
* 假设0代表店,1代表花 由于最后一次肯定是花,对前9次进行排列组合
*/
int min = 0xF;// 000001111
int max = 0x1E0;// 111100000

Map<String[], Double[]> map = new HashMap<String[], Double[]>();

//通过正则确定数据有效,有且只有4个1的二进制数为有效
Pattern p = Pattern.compile("[0]*1[0]*1[0]*1[0]*1[0]*");
// 排列出000001111~111100000之间所有可能
for (int i = min; i <= max; i++) {
//最后一次确定为花,所以固定为1
double alcohol = 1;
Double[] path = new Double[10];
path[0] = alcohol;

String str = Integer.toBinaryString(i);
Matcher m = p.matcher(str);
if (m.matches()) {
// 小于9位,前面补0
while (str.length() < 9) {
str = "0" + str;
}
String[] strArr = str.split("");
//最后一次确定为花
strArr[0] = "1";
//循环从1开始,跳过最后一次,也就是最后喝的那次
for (int j = 1; j < strArr.length; j++) {
if ("1".equals(strArr[j])) {
++alcohol;
} else {
alcohol = alcohol / 2;
}
path[j] = alcohol;
}
map.put(strArr, path);
} else {
// 不是4个1的为非法数据
continue;
}
}

//此处皆为打印结果,与逻辑无关
Set<Entry<String[], Double[]>> rSet = map.entrySet();
int cnt = 0;
for (Entry<String[], Double[]> entry : rSet) {
System.out.print(++cnt+":");
String[] strKey = entry.getKey();
Double[] dValue = entry.getValue();
for (int i = 0; i < strKey.length; i++) {
if ("1".equals(strKey[i])) {
System.out.print("[花:");

} else {
System.out.print("[店:");
}
System.out.print(dValue[i] + "] <-");
}
System.out.println();
}

}


xiezhenxiang 2010-04-08
  • 打赏
  • 举报
回复
学习了
sd01397055 2010-04-08
  • 打赏
  • 举报
回复

public class Test{
public static void main(String[] args){
float m=0;
for(int i=5;i>0;i--){
m = m/2+1;
}
System.out.println(m);


}
}
lzali 2010-04-08
  • 打赏
  • 举报
回复
关注··
zoeg 2010-04-08
  • 打赏
  • 举报
回复
1斗是2L,好几瓶白酒呢!跑业务好肚子啊!!!
TillPerfect 2010-04-08
  • 打赏
  • 举报
回复
这个五遇花和店,是得找个合理的理解。。。我也选择五次、五次吧。。。
遇到的顺序不同,的确影响结果。。。最近忙,好久没来逛了。该问题先存着,有空我要想想,好玩儿啊
afunx 2010-04-08
  • 打赏
  • 举报
回复
楼主,请问一下,这个酒的最小单位是多少啊?
比如,1斗等于10升,但1升又等于1000毫升。
你这题中,最小单位是多少呢?
justlearn 2010-04-08
  • 打赏
  • 举报
回复
[Quote=引用 30 楼 zoeg 的回复:]
X,居然已经有人这么做了,开始没注意看,害我还瞎折腾,献丑献丑,绝不是抄袭哈!!!
26楼的英雄,承让了!!!
[/Quote]
思路和我差不多,哈哈
不过你这个应该效率高点。我是想最后打个列表漂亮点,把一路过程都显示出来
zoeg 2010-04-08
  • 打赏
  • 举报
回复
X,居然已经有人这么做了,开始没注意看,害我还瞎折腾,献丑献丑,绝不是抄袭哈!!!
26楼的英雄,承让了!!!
  • 打赏
  • 举报
回复

高深!!!!!!
zoeg 2010-04-08
  • 打赏
  • 举报
回复
我是27楼,运行结果如下:
1:0.15625
2:0.1875
3:0.21875
4:0.25
5:0.28125
6:0.25
7:0.28125
8:0.3125
9:0.34375
10:0.34375
11:0.375
12:0.40625
13:0.4375
14:0.46875
15:0.53125
16:0.375
17:0.40625
18:0.4375
19:0.46875
20:0.46875
21:0.5
22:0.53125
23:0.5625
24:0.59375
25:0.65625
26:0.59375
27:0.625
28:0.65625
29:0.6875
30:0.71875
31:0.78125
32:0.8125
33:0.84375
34:0.90625
35:1.03125
36:0.625
37:0.65625
38:0.6875
39:0.71875
40:0.71875
41:0.75
42:0.78125
43:0.8125
44:0.84375
45:0.90625
46:0.84375
47:0.875
48:0.90625
49:0.9375
50:0.96875
51:1.03125
52:1.0625
53:1.09375
54:1.15625
55:1.28125
56:1.09375
57:1.125
58:1.15625
59:1.1875
60:1.21875
61:1.28125
62:1.3125
63:1.34375
64:1.40625
65:1.53125
66:1.5625
67:1.59375
68:1.65625
69:1.78125
70:2.03125
71:1.125
72:1.15625
73:1.1875
74:1.21875
75:1.21875
76:1.25
77:1.28125
78:1.3125
79:1.34375
80:1.40625
81:1.34375
82:1.375
83:1.40625
84:1.4375
85:1.46875
86:1.53125
87:1.5625
88:1.59375
89:1.65625
90:1.78125
91:1.59375
92:1.625
93:1.65625
94:1.6875
95:1.71875
96:1.78125
97:1.8125
98:1.84375
99:1.90625
100:2.03125
101:2.0625
102:2.09375
103:2.15625
104:2.28125
105:2.53125
106:2.09375
107:2.125
108:2.15625
109:2.1875
110:2.21875
111:2.28125
112:2.3125
113:2.34375
114:2.40625
115:2.53125
116:2.5625
117:2.59375
118:2.65625
119:2.78125
120:3.03125
121:3.0625
122:3.09375
123:3.15625
124:3.28125
125:3.53125
126:4.03125
加载更多回复(23)
相关推荐

62,568

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告