62,584
社区成员




/**
* “五遇花和店”,存在歧义,到底处理了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());//输出
}
}
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]");
}
}
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();
}
}