Programming Assignment 4: Input and Output

Here is the original link

  • ShannonEntropy.java(香农熵,信息论的重要基础)
public class ShannonEntropy
{
    public static void main(String[] args) {
        int m = Integer.parseInt(args[0]);
        int amount = 0;
        int[] count = new int[m + 1];
        double H = 0.0;
        while (!StdIn.isEmpty())
        {
            int x = StdIn.readInt();
            count[x]++;
            amount++;
        }
        for (int i = 1; i <= m; i++)
        {
            if (count[i] == 0) continue;
            H -= (Math.log(count[i] * 1.0 / amount) / Math.log(2)) * count[i] / amount;
        }
        StdOut.printf("%.4f\n", H);
    }
}

  • Checkerboard.java (调用StdDraw库绘制)
public class Checkerboard
{
    public static void main(String[] args) {
        int n = Integer.parseInt(args[0]);
        StdDraw.setScale(0, n);
        boolean isBlue = true;
        for (int j = 0; j < n; j++)
        {
        for (int i = 0; i < n; i++)
            {
                if (isBlue)
                {
                    StdDraw.setPenColor(StdDraw.BLUE);
                }
                else
                {
                    StdDraw.setPenColor(StdDraw.LIGHT_GRAY);
                }
                StdDraw.filledSquare(.5 + i, .5 + j, 0.5);
                isBlue = !isBlue;
            }
            if (n % 2 == 0)
            {
                isBlue = !isBlue;
            }
        }
    }
}

这里是绘制国际象棋棋盘,如图

  • WorldMap.java(调用StdDraw库绘制)
public class WorldMap
{
    public static void main(String[] args) {
        int width  = StdIn.readInt();
        int height = StdIn.readInt();
        StdDraw.setCanvasSize(width, height);
        StdDraw.setXscale(0, width);
        StdDraw.setYscale(0, height);
        while (!StdIn.isEmpty())
        {
            String region = StdIn.readString();
            int vertices = StdIn.readInt();
            double[] x = new double[vertices];
            double[] y = new double[vertices];
            for (int i = 0; i < vertices; i++)
            {
                x[i] = StdIn.readDouble();
                y[i] = StdIn.readDouble();
            }
            StdDraw.polygon(x, y);
        }
    }
}

这里是绘制美国地图,如图

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值