日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

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

發布時間:2024/8/26 java 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【蓝桥杯Java_C组·从零开始卷】第三节(附)、for循环练习题(数据题与图形题) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

目錄

一、基礎數據

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循环练习题(数据题与图形题)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。