• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
Icy.warrior
博客园    首页    新随笔    联系   管理    订阅  订阅
【第三天打卡。

BinarySearch

import java.util.Array

public class BinarySearch {
    public static int rank (int key, int[] a) {
        if (lo > hi) return -1;
        int lo = 0;
        int hi = a.length - 1;
        int mid = lo + (hi - lo) / 2;
        while (lo <= hi) {
        if (key < a[mid]) hi = mid - 1;
        else if (key > a[mid]) lo = mid + 1;
        else return mid;
        }
        return -1;
    }
    public static void main(String []args) {
        int[] whitelist = In.readInts(arg[0]);
        Array.sort (whitelist);
        while (!StdIn.isEmpty()) {
            int key = StdIn.readInt();
            if (rank (key, whitelist < 0)) {
                StdOut.println(key);
            }
        }
    }
}
View Code

Counter client that stimulates T coin flips

public class Counter {
    Counter (String id);
    void increment();
    int tally();
    String toString();
}

public class Flips {
    public static void main (String[] args) {
        int T = Integer.parseInt(args[0]);
        Counter heads = new Counter("heads");
        Counter tails = new Counter("tails");
        for (int t = 0; t < T; t ++) {
            if (StdRandom.bernoulli(0.5))
                heads.increment();
            else tails.increment();
        }
        StdOut.println(heads);
        StdOut.println(tails);
        int d = heads.tally() - tails.tally();
        StdOut.println(Math.abs(d));
    }
}
View Code

 

posted on 2017-08-05 09:57  Icy.warrior  阅读(107)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3