一、练习--黄金分割点

题目:

寻找某两个数相除,其结果 离黄金分割点 0.618最近

分母和分子不能同时为偶数

分母和分子 取值范围在[1,20] (即1到20)

要求效果:

public class HelloWorld {

public static
void main(String[] args) {

// 寻找某两个数相除,其结果 离黄金分割点 0.618最近

// 分母和分子不能同时为偶数

// 分母和分子 取值范围在[1-20]

int range = 20; // 取值范围

float breakPoint = 0.618f; // 黄金分割点

float minDiff = 100; // 离黄金分割点的差值

int answerFenzi = 0; // 找到的分子

int answerFenmu = 0; // 找到的分母

for (int fenzi = 1; fenzi <= range; fenzi++) {

for (int fenmu = 1; fenmu <= range; fenmu++) {

// 分母和分子不能同时为偶数

if (0 == fenzi % 2 & 0 == fenmu % 2)

continue;

// 取值

float value = (float) fenzi / fenmu;

// 取离黄金分割点的差值

float diff = value - breakPoint;

// 绝对值

diff = diff < 0 ? 0 - diff : diff;

// 找出最小的差值

if (diff < minDiff) {

minDiff = diff;

answerFenzi = fenzi;

answerFenmu = fenmu;

}

}

}

System.out.println("离黄金分割点(" + breakPoint + ")最近的两个数相除是:" + answerFenzi + "/" + answerFenmu + "="

+ ((float) answerFenzi / answerFenmu));

}

}

二、练习--水仙花数

水仙花数定义:

1. 一定是3位数

2. 每一位的立方,加起来恰好是这个数本身,比如153=1*1*1+5*5*5+3*3*3

寻找所有的水仙花数

要求效果:

public class HelloWorld {

public static
void main(String[] args) {

for (int i = 100; i < 1000; i++) {

int baiwei = i / 100;

int shiwei = i / 10 % 10;

int gewei = i % 10;

int cube = baiwei * baiwei * baiwei + shiwei * shiwei * shiwei + gewei * gewei * gewei;

if (cube == i) {

System.out.println("找到水仙花数:" + i);

}

}

}

}

三、练习--小学算术题

题目:

提示使用多层循环嵌套解决下图中的问题

官方答案:

public class HelloWorld {

public static
void main(String[] args) {

int a = 0;

int b = 0;

int c = 0;

int d = 0;

for (a = -100; a <= 100; a++) {

for (b = -100; b <= 100; b++) {

for (c = -100; c <= 100; c++) {

for (d = -100; d <= 100; d++) {

if(a+b==8&&c-d==6&&a+c==14&&b+d==10)
{

System.out.println("a:"+a);

System.out.println("b:"+b);

System.out.println("c:"+c);

System.out.println("d:"+d);

}

}

}

}

}

}

}

Java知识系统回顾整理01基础05控制流程08综合练习的更多相关文章

  1. Java知识系统回顾整理01基础05控制流程06break

    一.break break:结束循环 二.练习--直接结束当前for循环 public class HelloWorld { public static void main(String[] args ...

  2. Java知识系统回顾整理01基础05控制流程01if

    一.if if(表达式1){ 表达式2: } 如果表达式1的值是true, 就执行表达式2 public class HelloWorld { public static void main(Stri ...

  3. Java知识系统回顾整理01基础05控制流程07结束外部循环

    一.break是结束当前循环 二.结束当前循环实例 break; 只能结束当前循环 public class HelloWorld { public static void main(String[] ...

  4. Java知识系统回顾整理01基础05控制流程04 for

    一.for 比较for和while public class HelloWorld { public static void main(String[] args) { //使用while打印0到4 ...

  5. Java知识系统回顾整理01基础05控制流程03 while

    while和do-while循环语句 一.while:条件为true时 重复执行 只要while中的表达式成立,就会不断地循环执行 public class HelloWorld { public s ...

  6. Java知识系统回顾整理01基础05控制流程02 switch

    一.switch switch 语句相当于 if else的另一种表达方式 switch可以使用byte,short,int,char,String,enum 注: 每个表达式结束,都应该有一个bre ...

  7. Java知识系统回顾整理01基础05控制流程05 continue

    continue:继续下一次循环 一.continue 题目: 如果是双数,后面的代码不执行,直接进行下一次循环 要求效果: 答案: public class HelloWorld { public ...

  8. Java知识系统回顾整理01基础04操作符05赋值操作符

    一.赋值操作 赋值操作的操作顺序是从右到左 int i = 5+5; 首先进行5+5的运算,得到结果10,然后把10这个值,赋给i public class HelloWorld { public s ...

  9. Java知识系统回顾整理01基础03变量05变量命名规则

    一.命名规则 变量命名只能使用字母 .数字. $. _ 变量第一个字符 只能使用: 字母. $. _ 变量第一个字符 不能使用数字 注:_ 是下划线,不是-减号或者-- 破折号 int a= 5; i ...

随机推荐

  1. 微信小程序如何快速开通流量主

    1.先开发小程序,小程序需要有亮点,毕竟新颖(这样别人才更好去点击查看) 2.条件是独立访客(UV)不低于1000,1000人说多不多,说少也不少,因为小程序是没有链接的,是不可以进行一个流量刷取的, ...

  2. 分享几个好用的ui框架,以便开发

    1:Layui--经典模块化前端框架 地址:https://www.layui.com/ 2:iview--基于 Vue.js 的高质量 UI 组件库 地址:http://v1.iviewui.com ...

  3. Java中的IO操作和缓冲区

    目录 Java中的IO操作和缓冲区 一.简述 二.IO流的介绍 什么是流 输入输出流的作用范围 三.Java中的字节流和字符流 字节流 字符流 二者的联系 1.InputStreamReader 2. ...

  4. Myabtis动态SQL,你真的会了吗?

    目录 前言 什么是动态SQL? 常用的标签 if choose.when.otherwise where foreach set sql include 总结 拓展一下 Mybatis中如何避免魔数? ...

  5. 基于django快速开发一个网站(一)

    基于django快速开发一个网站(一) *  创建虚拟环境.基于虚拟环境创建django==2.0.0和图片加载库和mysql数据库驱动 1. 创建目录并创建虚拟环境 ╰$ mkdir Cornuco ...

  6. Selenium文件上传问题

     

  7. java-介绍函数理解重载

    package day02; public class FunctionOverload { public static void main(String[] args){ int a = add(, ...

  8. 新三种求数列中值SQL之效率再比拼

    在  https://www.cnblogs.com/xiandedanteng/p/12677688.html 中我列举了三种求中值方案,其中日本人MICK的做法因为不适用于二百万结果集而放弃,取而 ...

  9. 求学生单科流水表中单科最近/最新的考试成绩表的新增可行性方案 使用Oracle提供的分析函数rank

    在 https://www.cnblogs.com/xiandedanteng/p/12327809.html 一文中,提到了三种求学生单科最新成绩的SQL可行性方案,这里还有另一种实现,那就是利用分 ...

  10. 用c语言处理文件

    用c语言处理文件只需要用到几个简单的函数: 1.文件的打开和关闭 fopen()函数用来打开一个文件,该函数原型在头文件stdio.h中,调用的一般形式为 /* FILE 是c语言内置的一个结构体类型 ...