手记

#5.求最接近黄金分割点0.618的组合数

public class TestNNN {
    public static void main(String[] args) {
        int a = 0;
        int b = 0;
        float min = 100;
        for (int i = 1; i <= 20; i++) {
            for (int j = 1; j <= 20; j++) {
                if (i % 2 == 0 && j % 2 == 0)
                    continue;
                //(float)(i/j):先做除法,再将商转为float;(float)i / j:先做类型转换,再做除法
                float value = (float)i/j - 0.618f;
                value = value < 0 ? 0 - value : value;
                if (value < min) {
                    min = value;
                    a = i;
                    b = j;
                }
            }
        }
        System.out.println("最接近的组合是:" + a + "/" + b + " = " + (float)a/b);
    }
}

0人推荐
随时随地看视频
慕课网APP