读心术第七个测试数据是否为1-1000


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 = 0;
        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:55:47

    恰饭去了?

    • @ 2019-04-11 20:22:26

      保证在1-1000,没有0或更小,也没有1001或更大

      ps:还出差在外地呢…到北京就11点了

  • 1