学习使我自省!
一、流程控制
// 整合
// 流程控制
// 顺序-代码一行一行执行-常用
// 选择-判断条件,符合条件的代码块执行-if else
// 循环-循环执行某一代码块-for while do…while
二、练习之路-自省之路
1、黄金分割点0.618
// 九头身,黄金比例 头-肚脐:肚脐-脚趾
// 黄金分割点 0.618
// 618年中大促是不是这么来的?
// 问:求最接近黄金分割点 0.618的分数,分子分母不能同时为偶数,范围(0,20)
float gold = 0.618f; // 黄金分割点
float t = 0.0f; // 分割值
float last = 0.0f; // 最小差值
float first = 0.0f; // 临时差值
int a = 0,b = 0; // 索引
for(int i=1; i<20; i++){
for(int j=1; j<20; j++){
if(0 == i%2 && 0 == j%2){
continue;// 不能同时为偶数,继续下一循环
}
t = i/(float)j; // 分割值
if(i==1 && j==1){
last = t-gold; // 最小差值 赋初值
}
first = t - gold; // 临时差值
if(Math.abs(first) < Math.abs(last) ){ // abs() 求绝对值
last = first; // 更新最小差值
a = i; // 更新索引
b = j; // 更新索引
}
}
}
System.out.println(a + "/" + b + " = " + a/(float)b); // 最近黄金分割点
运行:
2、水仙花数
// 水仙花数 3位数
// 153 = 1*1*1 + 5*5*5 + 3*3*3 = 每一位立方之和
// 数学这么美!像花一样!
// 问:输出所有水仙花数
int b = 0; // 百位
int s = 0; // 十位
int g = 0; // 个位
for(int i=100; i<999; i++){
b = i/100;
s = i/10%10;
g = i%100%10;
if(i == Math.pow(b, 3) + Math.pow(s, 3) + Math.pow(g, 3))
System.out.print(i + " ");
}
System.out.println();
运行:
3、小学数学(博大精深)
// 猜猜我是谁
int a = 0;
int b = 0;
int c = 0;
int d = 0;
for (a = 0; a <= 100; a++) {
for (b = 0; b <= 100; b++) {
for (c = 0; c <= 100; c++) {
for (d = 0; d <= 100; d++) {
if (a + b == 8 && c - d == 6 && a + c == 14 && b + d == 10) {
System.out.println(String.format("%02d", a) + "+" + String.format("%02d", b) + "=" + (String.format("%02d", a+b)));
System.out.println(String.format("%02d", c) + "+" + String.format("%02d", d) + "=" + (String.format("%02d", c-d)));
System.out.println(String.format("%02d", a) + "+" + String.format("%02d", c) + "=" + (String.format("%02d", a+c)));
System.out.println(String.format("%02d", b) + "+" + String.format("%02d", d) + "=" + (String.format("%02d", b+d)));
}
}//d
}//c
}//b
}//a
运行:
三、学习源头
更多内容,点击了解:https://how2j.cn/k/control-flow/control-flow-practise/656.html?p=114999