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);
}
}