借助小学数学题复习java基础

今天看到一个小学数学题----"1-99中随意选两个数, 两值不相等,且和小于100,问有多少中可能.".

解法一, 编程思维(普通程序员).

双重for循环.if判断.集合去重. 硬编码, 简单快速

则,得解...

解法二,数学思维(学霸程序员).

1+2,1+3,...1+98 --> 97种情况

2+3,2+4....2+97 --> 95种情况

...

49+50                --> 1种情况.

应用 数学归纳法, 得 等差数列求和的公式 借助小学数学题复习java基础

则,得解...  不需要程序.

解法三.编程思维(2b程序员).

最后剩下的就是这种情况.把简单问题极度复杂化的情况. 如下代码.

(面向对象,集合类,jdk8..)

package com.dudo.dailytest;

import java.util.*;
import java.util.concurrent.CopyOnWriteArraySet;

/**
 * Created by zkai on 2014/12/3.
 */
public class PrimarySchoolTopic {
    private class Nums implements Comparable {
        private int a;
        private int b;

        public Nums(int a, int b) {
            this.a = a;
            this.b = b;
        }

        public boolean verify() {
            return verifyNum(a) && verifyNum(b) && a != b && a + b < maxSum;
        }

        private boolean verifyNum(int i) {
            return i >= min && i <= max;
        }

        @Override
        public int hashCode() {
            return getStr().hashCode();
        }

        private String getStr() {
            String format;
            if (a > b) {
                format = String.format("%s+%s=%s", b, a, a + b);
            } else {
                format = String.format("%s+%s=%s", a, b, a + b);
            }
            return format;
        }

        @Override
        public String toString() {
            return getStr();
        }

        @Override
        public boolean equals(Object obj) {
            if (obj == null || !(obj instanceof Nums)) {
                return false;
            }
            Nums two = (Nums) obj;
            return (this.a == two.a && this.b == two.b)
                    || (this.a == two.b && this.b == two.a);
        }

        // case 2
        @Override
        public int compareTo(Object obj) {
            if (obj == null || !(obj instanceof Nums)) {
                return -1;
            }
            Nums two = (Nums) obj;
            return two.a == a ?
                    two.b == b ?
                            0 :
                            two.b - b :
                    two.a - a;
        }
    }

    public static void main(String[] args) {
        PrimarySchoolTopic topic = new PrimarySchoolTopic(1, 99, 100);
        topic.compute().print();
    }

    private int min;
    private int max;
    private int maxSum;
    Set<Nums> numCount = new HashSet<>(); // case1 需要实现hashCode().equals().
//    Set<Nums> numCount = new TreeSet<>(); // case2 需要实现 Comparable.compareTo() [或在构造时传入 Comparator]
//    Set<Nums> numCount = new CopyOnWriteArraySet<>(); // case3 仅需要 equals().

    public PrimarySchoolTopic(int min, int max, int maxSum) {
        this.min = min;
        this.max = max;
        this.maxSum = maxSum;
    }

    public PrimarySchoolTopic compute() {
        for (int i = min; i <= max; i++) {
            for (int j = min; j <= max; j++) {
                Nums nums = new Nums(i, j);
                if (nums.verify()) {
                    numCount.add(nums);
                }
            }
        }
        return this;
    }

    public int size() {
        return numCount.size();
    }

    public PrimarySchoolTopic print() {
        numCount.forEach(System.out::println);
        System.out.println(numCount.size());
        return this;
    }
}

=========== 华丽分割线 以下为java复习内容 ==========================

1. hashSet       ---      equals. hashCode.

2. treeSet         ---      比较器

3. arrayset        ---      equals

4. jdk8 foreach

5. jdk8 function 编程.

6. .....

相关推荐