每日学习-Java基础(七)流程控制7(综合练习)

学习使我自省!
一、流程控制

    // 整合
	// 流程控制
	// 顺序-代码一行一行执行-常用
	// 选择-判断条件,符合条件的代码块执行-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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值