目录

一、基础数据

1、斐波那契数列

2、水仙花数

3、大马驮粮食

4、过路口

5、叠纸

二、图形文打印

1、打印正方形

2、左直角三角形

3、右直角三角形

4、等腰三角形

5、倒左直角三角形

6、倒右直角三角形

7、菱形

8、杨辉三角

9、九九乘法表(直角四个方向)


一、基础数据

1、斐波那契数列

斐波那契数列又称 黄金分割 数列,因数学家莱昂纳多·斐波那契(Leonardoda Fibonacci)以兔子繁殖为例子而引入,故又称为“ 兔子数列 ”;

* 指的是这样一个数列:1、1、2、3、5、8、13、21、34、……
* 其规律是从第3个数开始,每个数都等于它前两个数的和。

package Action;public class demos {public static void main(String[] args) {int month=10;int x=0;//从0开始计算int y=1;int z=1;System.out.println(y);//第一个月for(int i=1;i<month;i++) {z=x+y;     x=y;y=z;System.out.println(z);}}
}

2、水仙花数

输出所有的“水仙花数”,所谓“水仙花数”是指一个3位数,其各位数字的立方和等于该数本身

package Action;public class demos {public static void main(String[] args) {// 使用循环得遍历所有三位数for (int i = 100; i < 1000; i++) {// 分别定义三个变量获取该数的个、十、百位。int a = i % 10;int b = i / 10 % 10;int c = i / 100;// 判断该数是否为水仙花数if (a * a * a + b * b * b + c * c * c == i) {System.out.println(i + "是一个水仙花数");}}}
}

153是一个水仙花数
370是一个水仙花数
371是一个水仙花数
407是一个水仙花数

3、大马驮粮食

大马驮2石粮食,中马驮1石粮食,两头小马驮一石粮食,要用100匹马,驮100石粮食,该如何调配?

package Action;public class demos {public static void main(String[] args) {for (int a = 0; a <= 50; a++) {for (int b = 0; b <= 100; b++) {for (int c = 0; c <= 100; c++) {if (a + b + c == 100 && 2 * a + 1 * b + 0.5 * c == 100) {System.out.println("大马" + a + "中马" + b + "小马" + c);}}}}}
}

大马0中马100小马0
大马1中马97小马2
大马2中马94小马4
大马3中马91小马6
大马4中马88小马8
大马5中马85小马10
大马6中马82小马12
大马7中马79小马14
大马8中马76小马16
大马9中马73小马18
大马10中马70小马20
大马11中马67小马22
大马12中马64小马24
大马13中马61小马26
大马14中马58小马28
大马15中马55小马30
大马16中马52小马32
大马17中马49小马34
大马18中马46小马36
大马19中马43小马38
大马20中马40小马40
大马21中马37小马42
大马22中马34小马44
大马23中马31小马46
大马24中马28小马48
大马25中马25小马50
大马26中马22小马52
大马27中马19小马54
大马28中马16小马56
大马29中马13小马58
大马30中马10小马60
大马31中马7小马62
大马32中马4小马64
大马33中马1小马66

4、过路口

假设某人有100,000现金.每经过一次路口需要进行一次交费. 交费规则为当他现金大于50,000时每次需要交5%如果现金小于等于50,000时每次交5,000.请写一程序计算此人可以经过多少次这个路口。

package Action;public class demos {public static void main(String[] args) {double money = 100000;int count = 0;while (money >= 5000) {if (money > 50000) {money -= money * 0.05;} else {money -= 5000;}count++;}System.out.println(count);System.out.println(money);}
}

23
3767.497911552986

5、叠纸

一张纸的厚度大约是0.08mm,对折多少次之后能达到珠穆朗玛峰的高度(8848.13米)?

package Action;public class demos {public static void main(String[] args) {double len=0.08/1000;//mm换算成mdouble maxLen=8848.13;int count=0;while (true) {len*=2;count++;if(len>=maxLen) {break;}}System.out.println(count);}
}

二、图形文打印

1、打印正方形

*  *  *  *  * 
*  *  *  *  * 
*  *  *  *  *
*  *  *  *  * 
*  *  *  *  *

package Action;public class demos {public static void main(String[] args) {for(int i=0;i<5;i++) {//外循环for(int j=0;j<5;j++) {//内循环System.out.print("* ");//内循环执行一次,打印一个*}System.out.println();//换行}}
}

2、左直角三角形

*
        * *
        * * *
        * * * *
        * * * * *

package Action;public class demos {public static void main(String[] args) {for(int i=1;i<=5;i++) {for(int j=0;j<i;j++) {System.out.print("*");}System.out.print("\n");}}
}

3、右直角三角形

*
              * *
            * * *
          * * * *
        * * * * *

package Action;public class demos {public static void main(String[] args) {for(int i=1;i<=5;i++){for(int j=5;j>i;j--){//空格三角System.out.print(" ");}for(int k=0;k<i;k++){//星号三角System.out.print("*");}System.out.print("\n");}}
}

4、等腰三角形

*
           * * *
         * * * * *
      * * * * * * *
    * * * * * * * * *

package Action;public class demos {public static void main(String[] args) {for (int i = 1; i <= 5; i++) {for (int k = 5; k > i; k--) {System.out.print(" ");}for (int j = 0; j < 2 * i - 1; j++) {System.out.print("*");}System.out.print("\n");}}
}

5、倒左直角三角形

* * * * *
         * * * * 
         * * *  
         * * 
         *

package Action;public class demos {public static void main(String[] args) {for(int i=0;i<5;i++) {for(int j=5;j>i;j--) {System.out.print("*");}System.out.print("\n");}}
}

6、倒右直角三角形

* * * * * 
           * * * *
             * * *
               * *
                 *

package Action;public class demos {public static void main(String[] args) {for(int i=0;i<5;i++) {for(int k=0;k<i;k++){System.out.print(" ");}for(int j=5;j>i;j--) {System.out.print("*");            }       System.out.print("\n");}}
}

7、菱形

*
           * * *
         * * * * *
      * * * * * * *
    * * * * * * * * *
       * * * * * * * 
         * * * * * 
           * * * 
             *

package Action;public class demos {public static void main(String[] args) {for (int i = 1; i <= 5; i++) {for (int k = 5; k > i; k--) {System.out.print(" ");}for (int j = 0; j < 2 * i - 1; j++) {System.out.print("*");}System.out.print("\n");}for (int i = 5; i > 0; i--) {for (int k = 5; k >= i; k--) {System.out.print(" ");}for (int j = 2 * i - 3; j > 0; j--) {System.out.print("*");}System.out.print("\n");}}
}

8、杨辉三角

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
… … … …
杨辉三角的两个腰边的数都是 1,从第3行起,除第一个数和最后一个数外,其它位置的数都是上顶上两个数之和。

public static void main(String[] args) {Scanner sc = new Scanner(System.in);System.out.println("请输入你想打印多少行:");int n=sc.nextInt();getTriangle(n);}public static int[][] getTriangle(int n){int[][] arr=new int[n][n];      //创建二维数组行列数都为nfor (int  i = 0; i <arr.length ; i++) {for(int j=0;j<=i;j++){if(i==0||j==0||i==j){       //判断如果行数、列数为1,或行列数相等arr[i][j]=1;            //则赋值1}else{        //否则,其他位置的值 = 上一行前一列的值 + 上一行这一列的值arr[i][j]=arr[i-1][j-1]+arr[i-1][j];}}}//便利二维数组for(int i=0;i<arr.length;i++){for(int j=0;j<=i;j++){System.out.print(arr[i][j]+"\t");}System.out.println();}return arr;}

9、九九乘法表(直角四个方向)

package Action;public class demos {public static void main(String[] args) {// 左下角直角for (int i = 1; i <= 9; i++) {for (int j = 1; j <= i; j++) {System.out.print(j + "*" + i + "=" + i * j + " ");}System.out.print("\n");// 换行}// 左上角直角for (int i = 9; i >= 1; i--) {for (int j = 1; j <= i; j++) {System.out.print(j + "*" + i + "=" + i * j + " ");}System.out.print("\n");}// 右下角直角for (int i = 1; i <= 9; i++) {// 控制行// 用来控制每行空格的数量for (int k = 1; k <= 9 - i; k++) {System.out.print("\t");}for (int j = 1; j <= i; j++) {// 控制列System.out.print(j + "*" + i + "=" + (i * j) + "\t");}System.out.println();}// 右上角直角for (int i = 9; i >= 1; i--) {for (int k = 1; k <= 9 - i; k++) {System.out.print("\t");}for (int j = 1; j <= i; j++) {System.out.print(j + "*" + i + "=" + (i * j) + "\t");}System.out.println();}}
}

刷题的时候要注意细致很重要。

【蓝桥杯Java_C组·从零开始卷】第三节(附)、for循环练习题(数据题与图形题)相关推荐

  1. 【蓝桥杯Java_C组·从零开始卷】第四节(附)、字符串常用函数

    导读 本文章将java中字符串常用的字符串进行罗列与对应demo的示例,帮助java初学者与蓝桥杯参赛的选手提升对JavaSE的理解. 目录 字符串由来 字符串转成byte数组 常用字符串函数列表: ...

  2. 【蓝桥杯Java_C组·从零开始卷】第四节、一维数组与二维数组

    整篇文章为对java数组的完整理解以及部分排序,并有一些简单的demo,经典的案例与蓝桥杯的一些经典数组题有专门的文章梳理. 目录 数组概述 什么是数组 数组的结构 数组的特点: 数组分类 一维数组声 ...

  3. 【蓝桥杯Java_C组·从零开始卷】第三节、while循环do while循环for循环(超重点)break终止循环continue结束本次循环

    开篇主旨--循环四要素 初始值 表达式 循环体 迭代器 所有的循环无论怎么封装,都会遵循着这是个点,就算用hash的方法也算是迭代器的用法. 只要控制好这四个,那么您循环的功底也就满足了,后期递归啥的 ...

  4. 【蓝桥杯Java_C组·从零开始卷】第一节、环境与变量类型运算符与类型分析

    B站高清回放地址: [https://www.bilibili.com/video/BV1Bm4y1Q7Wt?spm_id_from=333.999.0.0] 目录 一.Java环境搭建与使用(Ecl ...

  5. 【蓝桥杯Java_C组·从零开始卷】第八节、集合——list详解(ArrayList、 LinkedList 和 Vector之间的区别)

    ArrayList. LinkedList 和 Vector之间的区别 ArrayList. LinkedList 和 Vector都实现了List接口,是List的三种实现,所以在用法上非常相似.他 ...

  6. 【蓝桥杯Java_C组·从零开始卷】第八节、集合——list详解

    由于咱们针对与算法使用,所有会有一些个函数可能平时用不到. 目录 list基础增 删 改 查 算法中最常用的交换 自然排序 反向排序·注不是倒序,不是倒序,不是倒序 随机排序 判断是否有某元素 集合截 ...

  7. 【蓝桥杯Java_C组·从零开始卷】第八节、综合测试

    1.利用[^]进行x与y两个变量的值交换,并写明注释 2.考试奖励 小明期末考试,爸爸承诺如果小明考了: 1.100 - 95分奖励小明山地自行车一辆: 2.94 - 90分奖励小明到游乐园玩一天: ...

  8. 【蓝桥杯Java_C组·从零开始卷】第七节、递归

    目录 递归概述 递归: 循环: 疑问: 是什么递归? 递归的精髓(思想)是什么? 递归的三要素 1). 明确递归终止条件 2). 给出递归终止时的处理办法 3). 提取重复的逻辑,缩小问题规模* 递归 ...

  9. 【蓝桥杯Java_C组·从零开始卷】第六节(二)、蓝桥杯常用数学公式

    目录 1.欧几里得定理 2.最大公约数 3.最小公倍数 4.海伦公式(求三角形面积) 5.排序公式 1.欧几里得定理 package Action;public class demo {/** 求最大 ...

最新文章

  1. 图灵奖得主LeCun力推无监督学习:要重视基于能量的学习方法
  2. springboot 集成logback
  3. SpringBoot系列二:搭建自己的第一个SpringBoot程序
  4. 登陆模块防止恶意用户客户端攻击
  5. dingo php,用laravel dingo/api创建简单的api
  6. asp.net中将枚举绑定到下拉列表
  7. 数据结构---B-(B)、B+的总结
  8. 浅谈SQLiteOpenHelper之onUpgrade例子
  9. 本地像服务器传文件,本地向服务器传送文件
  10. K2 Blackpearl中从数据库直接删除流程实例之K2Server表
  11. 持续改进----白狼族的故事(完结)
  12. Vector CANape的使用记录
  13. 【零散知识】核密度估计(Kernel Density Estimation)
  14. 内网渗透-内网代理穿透和内网横向移动
  15. 修改360浏览器模式为极速模式
  16. jenkins创建流水线,基于gitlab与Jenkinsfile
  17. SEO全称:Search Engine Optimization,即搜索引擎优化
  18. 陈延伟:任督二脉之内存管理总结笔记
  19. Qt 5.9.1 参考手册 QtTest 第5章 写一个基准线
  20. Vegas怎么做闪屏特效,闪屏特效制作教程

热门文章

  1. python 指定端口读取网站_Python实现局域网指定端口扫描
  2. php mysql查询时间_php查询时间段 mysql时间查询
  3. 机器学习-分类算法-线性回归、梯度下降,过拟合欠拟合,岭回归11
  4. 取后端数据_用 Flask+Axios 实现前后端数据通信:查询动森鱼类价格
  5. mybatis的插件分析
  6. 面试系列12 redis和memcached有什么区别
  7. [蓝桥杯]PREV-19.历届试题_九宫重排
  8. 初识react中高阶组件
  9. GoldenGate for Java adapter介绍二(代码篇)
  10. 排序算法(快速排序)