算法-第四版-练习1.2.10解答
来源:互联网 发布:成都算法工程师招聘网 编辑:程序博客网 时间:2024/06/06 04:11
编写一个类VisualCounter,支持加一和减一操作。它的构造函数接受两个参数N和max,其中N指定了 操作在最大次数,max指定了计数器的最大绝对值。作为副作用,用图像显示每次计数器变化后的值。
/** * Description : * Author : mn@furzoom.com * Date : Sep 26, 2016 5:04:27 PM * Copyright (c) 2013-2016, http://furzoom.com All Rights Reserved. */package com.furzoom.lab.algs.ch102;import edu.princeton.cs.algs4.StdDraw;import edu.princeton.cs.algs4.StdRandom;/** * ClassName : E10210 <br> * Function : TODO ADD FUNCTION. <br> * date : Sep 26, 2016 5:04:27 PM <br> * * @version */public class E10210{ public static void main(String[] args) { int max = 15; int count = 30; StdDraw.setXscale(0, count+1); StdDraw.setYscale(-max, max); StdDraw.line(0, 0, count+1, 0); StdDraw.setPenColor(StdDraw.RED); StdDraw.setPenRadius(0.005); VisualCounter counter = new VisualCounter(count, max); counter.draw(); for (int i = 0; i < count; i++) { if (StdRandom.bernoulli()) { counter.increment(); } else { counter.decrement(); } counter.draw(); } }}class VisualCounter{ private final int n; private final int max; private int value; private int operationCount; public VisualCounter(int n, int max) { this.n = n; this.max = max; this.value = 0; this.operationCount = 0; } public void increment() { if (operationCount++ < n && value < max) { value++; } } public void decrement() { if (operationCount++ < n && value > -max) { value--; } } public void draw() { StdDraw.point(operationCount, value); }}结果如下:
算法-第四版-1.2 数据抽象-习题索引汇总
算法-第四版习题索引汇总
0 0
- 算法-第四版-练习1.2.10解答
- 算法-第四版-练习1.2.1解答
- 算法-第四版-练习1.2.2解答
- 算法-第四版-练习1.2.3解答
- 算法-第四版-练习1.2.4解答
- 算法-第四版-练习1.2.5解答
- 算法-第四版-练习1.2.6解答
- 算法-第四版-练习1.2.7解答
- 算法-第四版-练习1.2.8解答
- 算法-第四版-练习1.2.9解答
- 算法-第四版-练习1.2.11解答
- 算法-第四版-练习1.2.12解答
- 算法-第四版-练习1.2.13解答
- 算法-第四版-练习1.2.14解答
- 算法-第四版-练习1.2.15解答
- 算法-第四版-练习1.2.16解答
- 算法-第四版-练习1.2.17解答
- 算法-第四版-练习1.2.18解答
- 日志记录 PHP下往linux目录下写日志
- 切换git地址
- JAVA8新特性(Lambda 表达式)
- 7. Reverse Integer
- APIcloud 按两次返回键退出程序,android按两次返回键退出程序
- 算法-第四版-练习1.2.10解答
- Swift - 使用EventKit操作"提醒事项"(2、新增、修改、删除提醒)
- mysql时间日期查询格式
- Codeforces Round #373 (Div. 2) C: Efim and Strange Grade
- Protostuff序列化工具类
- THINKPHP 伪静态 URL重写
- Gradle for Android 使用之旅之gradle的基础
- poj1664 放苹果
- Android Studio之Error:(158) Error: Expected resource of type id [ResourceType]