W1D3 图形打印练习题 ——新思路

流程控制练习题

基础题

图形打印:

*****		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();
}
增强题

  1. 求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);
  2. 已知 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);
    			}
    		}
    	}
    }
  3. 使用递归计算1+2+3+…+100

  4. 使用递归输出斐波那契数列前20位

1、一个猴子从树上摘下来若干香蕉,当即吃了一半,觉得不过瘾,又吃了一个。第二天早上吃了剩下的一半,又多吃了一个,以此类推,往后每一天的早上都是吃了剩余数量的一半多一个。到了第十天早上在吃的时候,发现只剩下一个了。问,这个猴子最开始摘了多少个香蕉。

2、有一对兔子,从第三个起,每个月生一对兔子。新的兔子也是从第三个月起,每个月生一对兔子。问从一个月算起,每个月的兔子有多少对。

1		1
2		1
3		2
4		3
5		5
6		8
。。。

3、圆桌报数
4、打印杨辉三角
1
11
121
1331
14641
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值