码迷,mamicode.com
首页 > 其他好文 > 详细

【第三天打卡。

时间:2017-08-05 10:59:46      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:while   incr   turn   log   nts   ber   else   blog   alt   

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

 

【第三天打卡。

标签:while   incr   turn   log   nts   ber   else   blog   alt   

原文地址:http://www.cnblogs.com/moonlightshadow/p/7289307.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!