流程控制练习题
基础题
图形打印:
***** 1 5
**** 2 4
*** 3 3
** 4 2
* 5 1
for (int line = 1; line <= 5; line++) {
for (int colum = 1; colum + line <= 6; colum++) {
System.out.print("* ");
}
System.out.println();
}
12345
1234
123
12
1
for (int line = 1; line <= 5; line++) {
for (int colum = 1; colum + line <= 6; colum++) {
System.out.print(colum + " ");
}
System.out.println();
}
* 1 4
** 2 3
*** 3 2
**** 4 1
***** 5 0
for (int line = 1; line <= 5; line++) {
for (int colum = 1; colum <= 5; colum++) {
if (colum + line <= 5) {
System.out.print(" ");
}
else {
System.out.print("* ");
}
}
System.out.println();
}
***** 1 0
**** 2 1
*** 3 2
** 4 3
* 5 4
for (int line = 1; line <= 5; line++) {
for (int colum = 1; colum <= 5; colum++) {
if (colum < line) {
System.out.print(" ");
}
else {
System.out.print("* ");
}
}
System.out.println();
}
* 1 5
*** 2 6
***** 3 7
******* 4 8
********* 5 9
for (int line = 1; line <= 5; line++) {
for (int colum = 1; colum - line <= 4; colum++) {
if (colum + line <= 5) {
System.out.print(" ");
}
else {
System.out.print("* ");
}
}
System.out.println();
}
*
***
*****
*******
*********
******* 1 8 1
***** 2 7 2
*** 3 6 3
* 4 5 4
for (int line = 1; line <= 5; line++) {
for (int colum = 1; colum - line <= 4; colum++) {
if (colum + line <= 5) {
System.out.print(" ");
}
else {
System.out.print("* ");
}
}
System.out.println();
}
for (int line = 1; line <= 4; line++) {
for (int colum = 1; colum + line <= 9; colum++) {
if (colum <= line) {
System.out.print(" ");
}
else {
System.out.print("* ");
}
}
System.out.println();
}
***** 1 9
***** 2 8
***** 3 7
***** 4 6
***** 5 5
for (int line = 1; line <= 5; line++) {
for (int colum = 1; colum + line <= 10; colum++) {
if (colum + line <= 5) {
System.out.print(" ");
}
else {
System.out.print("* ");
}
}
System.out.println();
}
ABCDE
BCDEF
CDEFG
DEFGH
EFGHI
for (int line = 1; line <= 5; line++) {
for (int colum = 1; colum - line <= 4; colum++) {
if (colum < line) {
System.out.print(" ");
}
else {
System.out.print((char)('A' + colum - 1));
}
}
System.out.println();
}
ABBBB
AABBB
AAABB
AAAAB
AAAAA
for (int line = 1; line <= 5; line++) {
for (int colum = 1; colum + line <= 10; colum++) {
if (colum + line <= 5) {
System.out.print(" ");
}
else {
System.out.print((colum <= 5 ? 'A' : 'B') + " ");
}
}
System.out.println();
}
增强题
-
求s=a+aa+aaa+aaaa+aa…a的值,其中a是一个数字(1-9之间)计算的数据的个数是5。例如2+22+222+2222+22222求和
int sum = 0; // 记录最终相加的和 int a = 2; // 需要相加的数字 int n = 5; // 相加的数字的个数 String str = "";// 拼接式子 // 累加 for (int i = 1; i <= n; i++) { // 计算每次累加的数字是谁 int tmp = 0; for (int j = 1; j <= i; j++) { tmp = tmp * 10 + a; } // 拼接式子 str += tmp + (i == n ? " = " : " + "); // 计算累加的结果 sum += tmp; } System.out.println(str + sum);
-
已知 abc+cba=1333, 其中的a,b,c均为一位数,编写一个程序,求出a,b,c分别代表什么数字
for (int i = 0; i <= 9; i++) { for (int j = 0; j <= 9; j++) { for (int k = 0; k <= 9; k++) { if (i*100+j*10+k + k*100+j*10+i == 1333) { System.out.println(i + "" + j + "" + k); } } } }
-
使用递归计算1+2+3+…+100
-
使用递归输出斐波那契数列前20位
1、一个猴子从树上摘下来若干香蕉,当即吃了一半,觉得不过瘾,又吃了一个。第二天早上吃了剩下的一半,又多吃了一个,以此类推,往后每一天的早上都是吃了剩余数量的一半多一个。到了第十天早上在吃的时候,发现只剩下一个了。问,这个猴子最开始摘了多少个香蕉。
2、有一对兔子,从第三个起,每个月生一对兔子。新的兔子也是从第三个月起,每个月生一对兔子。问从一个月算起,每个月的兔子有多少对。
1 1
2 1
3 2
4 3
5 5
6 8
。。。
3、圆桌报数
4、打印杨辉三角
1
11
121
1331
14641