读心术如何七次猜出答案

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
  Scanner in=new Scanner(System.in);
  int daan=in.nextInt();
  int zuixiao=1;
  int zuida=1000;
  int caishu=500;
  int cishu=0;
  while(caishu!=daan) {
      caishu=(zuixiao+zuida)/2;
      cishu=cishu+1;
      System.out.println("第"+cishu+"次: "+caishu);
  if(caishu==daan) {
          break;
  } else if(caishu>daan) {
      zuida=caishu;
  }else if(caishu<daan){
      zuixiao=caishu;
  }
      
  
    }
    }
}

1 条评论

  • @ 2019-04-11 11:00:41

    GKD

    • @ 2019-04-11 11:03:10

      思路几乎是对的,错了一点点,而且是一个老程序员也常常出错的大bug。

      太好了,下周我要讲这个。

  • 1