《Java程序設計 一種跨學科的方法》第1章第2節練習

1.2.24 貸款付款

編寫一個程序,計算每月付款,你必須給出每年還貸的一個具體數字來指定的連續利率,使用來自命令行的3個參數,t,P和年利率r。需要計算的值的公式是Pe^rt。

public class Main {

    public static void main(String[] args) {
        double t = Double.parseDouble(args[0]);
        double p = Double.parseDouble(args[1]);
        double r = Double.parseDouble(args[2]);
        double v = p * Math.exp(r * t);
        System.out.println(v);
    }
}

1.2.25 風的溫度

假設溫度爲t(華氏),風速爲v(英里每小時),氣象局定義有效溫度(風的溫度)是:

W = 35.74 + 0.6215t + (0.4275t - 35.75) v^0.16

編寫一個小程序,使用來自於命令行的參數t和v,並打印風的溫度。

注意:如果t的絕對值大於50,或者如果v的值大於120或小於3時,這個公式是無效的。

public class Main {

    public static void main(String[] args) {
        double t = Double.parseDouble(args[0]);
        double v = Double.parseDouble(args[1]);
        if (Math.abs(t) > 50.0 || v > 120.0 || v < 3.0)
        {
            System.out.println("無效");
            return;
        }
        double w = 35.74 + 0.6215 * t + (0.4275 * t - 35.75) * Math.pow(v, 0.16);
        System.out.println(w);
    }
}

1.2.26 極座標

編寫一個將直角座標轉換爲極座標的程序。你的程序應該使用來自命令行的兩個實數x和y,然後打印極座標r和θ。

public class Main {

    public static void main(String[] args) {
        double x = Double.parseDouble(args[0]);
        double y = Double.parseDouble(args[1]);
        double r = Math.sqrt(x * x + y * y);
        double angle = Math.atan2(y, x);
        System.out.println(r + "," + angle);
    }
}
1.2.27 高斯隨機數

產生高斯分佈隨機數的一個方法就是使用Box-Muller公式:

w = sin(2лv)(-2lnu)^(1/2)

這裏u和v是使用Math.random()方法產生的0到1之間的實數,編寫一個程序打印所有標準的高斯隨機變量。

public class Main {

    public static void main(String[] args) {
        double u = Math.random();
        double v = Math.random();
        double w = Math.sin(2 * Math.PI * v) * Math.sqrt(-2 * Math.log(u));
        System.out.println(w);
    }
}
1.2.28 排序檢查

編寫一個程序,使用來自命令行參數double值x y和z,如果3個數的值是嚴格按照升序排序或降序排序,打印true,否則打印false。

public class Main {

    public static void main(String[] args) {
        double x = Double.parseDouble(args[0]);
        double y = Double.parseDouble(args[1]);
        double z = Double.parseDouble(args[2]);
        System.out.println(x < y && y < z || x > y && y > z);
    }
}
1.2.29 星期幾

編寫一個程序,使用日期作爲輸入,然後打印日期是星期幾。你的程序應該使用3個命令行參量:m(月) d(日) y(年)。對於m,使用1表示一月,使用2表示二月,依此類推。對於輸出,顯示0表示星期天,1表示星期一,2表示星期二,依此類推。

public class Main {

    public static void main(String[] args) {
        int m = Integer.parseInt(args[0]);
        int d = Integer.parseInt(args[1]);
        int y = Integer.parseInt(args[2]);
        int y0 = y - (14 - m) / 12;
        int x = y0 + y0/4 - y0/100 + y0/400;
        int m0 = m + 12 * ((14 - m) / 12) - 2;
        int d0 = (d + x + 31*m0/12) % 7;
        System.out.println(d0);
    }
}
1.2.30 均勻隨機數

編寫一個程序,打印在0到1之間的5個均勻隨機數的值,它們的平均值、最小值和最大值。

public class Main {

    public static void main(String[] args) {
        double a = Math.random();
        double b = Math.random();
        double c = Math.random();
        double d = Math.random();
        double e = Math.random();
        double average = (a + b + c +d +e) / 5;
        double max = Math.max(a, Math.max(b, Math.max(c, Math.max(d, e))));
        double min = Math.min(a, Math.min(b, Math.min(c, Math.min(d, e))));
        System.out.println(a + " " + b + " " + c + " " + d + " " +e);
        System.out.println("average=" + average);
        System.out.println("max=" + max);
        System.out.println("min=" + min);
    }
}
1.2.31 Mercator投影

Mercator投影是等角(角度保存)的投影,它將緯度φ和經度λ映射到直角座標(x,y)。Mercator投影正在被廣泛使用,例如,海圖和你可以從網上打印的地圖。這個投影的等式定義爲x = λ - λ0 和 y = ln((1+sinφ)/(1-sinφ)),這裏λ0中在地圖中心點的經度。編寫一個程序,使用來自命令行的λ0、點的緯度和經度,然後打印它的投影。

public class Main {

    public static void main(String[] args) {
        double λ0 = Double.parseDouble(args[0]);
        double λ = Double.parseDouble(args[1]);
        double φ = Double.parseDouble(args[2]);
        double x = λ - λ0;
        double y = Math.log((1 + Math.sin(φ)) / (1 - Math.sin(φ)));
        System.out.println(x + "," + y);
    }
}
1.2.32 顏色轉換

現在使用幾種不同格式表示顏色。例如,用於LCD顯示、數字照相機和網頁的主要格式稱爲RGB格式,指定從0到255的紅色r、綠色g和藍色b的整數。出版圖書和雜誌的主要格式是CMYK格式,從0.0到1.0的深藍c、洋紅m、黃y和黑k的實數。編寫一個程序,將RGB轉換爲CMYK。

public class Main {

    public static void main(String[] args) {
        int r = Integer.parseInt(args[0]);
        int g = Integer.parseInt(args[1]);
        int b = Integer.parseInt(args[2]);
        double c, m, y, k;
        if (r == 0 && g == 0 && b == 0) {
            c = m = y = 0;
            k = 1;
        } else {
            double w = Math.max(r/255.0, Math.max(g/255.0, b/255.0));
            c = (w - r/255.0) / w;
            m = (w - g/255.0) / w;
            y = (w - b/255.0) / w;
            k = 1 - w;
        }
        System.out.printf("(%f,%f,%f,%f)", c, m, y, k);
    }
}
1.2.33 大圓線

編寫一個程序,使用4個命令行參數x1,y1,x2,y2(在地球上兩點的緯度和經度),打印這兩點間大圓線的距離。計算大圓線的距離(英里)公式是:

d = 69.1105*arccps(sin(x1)sin(x2)+cos(x1)cos(x2)cos(y1-y2))

使用你的程序計算在巴黎(49.87度N和-2.33度W)和舊金山(37.8度N和122.4度W)之間的大圓線距離。

public class Main {

    public static void main(String[] args) {
        double x1 = Double.parseDouble(args[0]);
        double y1 = Double.parseDouble(args[1]);
        double x2 = Double.parseDouble(args[2]);
        double y2 = Double.parseDouble(args[3]);
        double d = 69.1105 * Math.acos(Math.sin(x1)*Math.sin(x2) + Math.cos(x1)*Math.cos(x2)*Math.cos(y1-y2));
        System.out.println(d);
    }
}
1.2.34 三個數排序

編寫一個程序,使用來自命令行的3個int值,然後以升序方法打印這3個數。使用Math.max和Math.min。

public class Main {

    public static void main(String[] args) {
        int a = Integer.parseInt(args[0]);
        int b = Integer.parseInt(args[1]);
        int c = Integer.parseInt(args[2]);
        int n1 = Math.min(a, Math.min(b, c));
        int n3 = Math.max(a, Math.max(b, c));
        int n2 = a;
        if (n2 == n1 || n2 == n3)
            n2 = b;
        if (n2 == n1 || n2 == n3)
            n2 = c;
        System.out.println(n1 + "," + n2 + "," + n3);
    }
}
1.2.35 Dragon曲線

編寫一個程序,打印畫0-5曲線的指令。指令是字符F、L、R,這裏F的意思是向前移動1個單位同時畫直線,L的意思是左轉,R的意思是右轉。當你將小紙條摺疊N次,然後以對角展開就形成了N階Dragon曲線。解決這個問題的關鍵是要注意到,階數爲N的曲線是階爲N-1的曲線後面是L再後面是N-1階曲線的反向移動,然後畫出相似的反向曲線。

public class Main {

    public static void main(String[] args) {
        String s0 = "F";
        String s1 = s0 + "L" + new StringBuffer(s0).reverse().toString().replace('L', '_').replace('R', 'L').replace('_', 'R');
        String s2 = s1 + "L" + new StringBuffer(s1).reverse().toString().replace('L', '_').replace('R', 'L').replace('_', 'R');
        String s3 = s2 + "L" + new StringBuffer(s2).reverse().toString().replace('L', '_').replace('R', 'L').replace('_', 'R');
        String s4 = s3 + "L" + new StringBuffer(s3).reverse().toString().replace('L', '_').replace('R', 'L').replace('_', 'R');
        System.out.printf("%s\n%s\n%s\n%s\n%s\n", s0, s1, s2, s3, s4);
    }
}


發佈了40 篇原創文章 · 獲贊 8 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章