1、黄金分割比
寻找某两个数相除,其结果 离黄金分割点 0.618最近
分母和分子不能同时为偶数
分母和分子 取值范围在[1-20]
public class GoldenPoint {
public static void main(String[] args) {
int a = 1, b = 1;
double goldenPoint = 0.618, result, tolerance, min = 1;
for(int i = 1; i <= 20; i++) {
for(int j = 1; j < i; j++) {
result = 1.0 * j / i;
tolerance = Math.abs(goldenPoint - result);
if(tolerance < min) {
min = tolerance;
a = j;
b = i;
}
}
}
System.out.print("离黄金分割点(0.618)最近的两个数相除是:");
System.out.println(a + "/" + b + "=" + 1.0 * a / b);
}
}
2、水仙花数
水仙花数定义:
- 一定是3位数
- 每一位的立方,加起来恰好是这个数本身,比如153=111+555+333
寻找所有的水仙花数
public class Practice {
public static void main(String[] args) {
int a=0;
int b=0;
int c=0;
for(int i=100;i<999;i++){
a=i/100;
b=(i-a*100)/10;
c=i%10;
if(i==a*a*a+b*b*b+c*c*c){
System.out.println(i);
}
}
}
}
3、小学算术题
提示使用多层循环嵌套解决
public class Computing {
public static void main(String[] args) {
int a = 0;
int b = 8-a;
int c = 14-a;
int d = a+2;
while((c-d)!=6){
a++;
b = 8-a;
c = 14-a;
d = a+2;
}
System.out.printf("a=%d%nb=%d%nc=%d%nd=%d",a,b,c,d);
}
}