李白无事街上走,提壶去买酒。遇店加一倍,见花喝一斗。五遇花和店,喝光壶中酒。试问李白壶中原有多少斗酒?
使用for循环结构编程实现敬请高手指点!感激不尽!

解决方案 »

  1.   

    使用倒推法,最后酒的数量为0,
    最后遇花喝一斗前:0+1=1; 
    最后遇店加一倍,则原有:1÷2=1/2;
    ......  double alohol=0;
    for (int i = 0; i < 5; i++) {
    alohol = ++alohol / 2;
    }
      

  2.   


    public class Test {

    public static void main(String[] args) {
    int dian = 0;
    int hua = 0;
    StringBuffer e = new StringBuffer();;

    //五遇店
    //每次打酒单位按照1升计算
    for (int i = 1; i < 6; i++) {
    dian += i * 2;
    }
    //五遇花
    //一斗酒等于10升
    for (int i = 1; i < 6; i++) {
    hua += 10; 
    }

    e.append("李白壶中原有:" + (hua - dian) / 10 + "斗酒");

    System.out.println(e.toString());
    }
    }
      

  3.   

    上面我写的循环等价于下面这个        double alohol=0;
            for (int i = 0; i < 5; i++) {    
               alohol = alohol + 1;//喝的酒吐出来
                alohol = alohol / 2;//加的酒倒出来
             }
      

  4.   

    我感觉结果应该是一个有穷的数组 数组的每个元素都是一种可能
    import java.util.*;
    public class LiBai { public static void main(String[] args) {
    //用倒推法
    int end = 0;
    int start  ;
    //遇到花flag为false  遇到店flag为true
    boolean flag = false;
    for(int i=0; i<5; i++){
    //最后遇到的肯定是花店
    end = end + 1;
    Random random = new Random();
    flag = random.nextBoolean();
    if(flag){
    end = end*2;
    }
    }
    start = end;
    System.out.println(start);
    }}
      

  5.   

    //李白饮酒问题
    public class Wire {
    public static void main(String[] args) {
    // 反推,最后一次遇花店
    double end = 0;
    // 倒数第二次为
    end = end + 1;
    for (int i = 0; i < 5; i++) {
    // 前9次为随机
    // Random ran = new Random();
    // System.out.println(ran.nextBoolean());
    end /= 2;
    end++;
    }
    System.out.println(end);
    }
    }
    运行结果:1.96875
    但是始终觉得有点问题,又没发觉问题在哪里
      

  6.   

    既然是说随机的遇店和花,我重新写了个,排列组合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();
    } }
      

  7.   

    简化了一下,最后用正序方式打印结果 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]");
    }
    }
      

  8.   

    /**
       * “五遇花和店”,存在歧义,到底处理了5次还是10次? 我跟着楼上各位的意思,按10次走吧!
       * 一共5处花,那么李酒仙一共喝完5斗酒(唐代1斗为2000mL,那就是10L,天啊!!!) !
       * 最后喝光了酒,说明最后一次是遇到花,那么只需要穷举4花5店的组合;
       * 题目既然指明要用for循环来解题,那么必定是外面一套迭代所有的可能
       * 里面一套计算该可能的结果:
       */
      public static void main(String args[]) throws Exception{
        int ccnt=1;
        for(int i=0xF;i<=0x1E0;i++){//使用9个二进制位穷举所有可能,1=花,0=店
          if(Integer.bitCount(i)==4){//当花数为4才符合条件(最后一个已经固定了)
            BigInteger bi=BigInteger.valueOf(i);//用这个测试位值,避免啰嗦的位移
            double n=1;//倒数第二次开始计算,应该有1斗酒
            for(int j=0;j<9;j++){
              if(bi.testBit(j))n++;
              else n/=2;
            }
            System.out.println(ccnt+++":"+n);
          }
        }
      }
      

  9.   


    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);
         }
    }
      

  10.   

    不过我发现输出的结果有重复,所以斗胆把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());//输出
      }
     }