2018HPU暑期集训第四次积分训练赛 K - 方框 题解(图形打印) 2018HPU暑期集训第四次积分训练赛 K - 方框 题解(图形打印)

2018HPU暑期集训第四次积分训练赛 K - 方框 题解(图形打印)

思路分析:题目已经明确透露了这道题的解法:就是画框。当 输入的边长  n - 4 > 0 的话,就表示可以在内层继续嵌套一个方框。废话就不多说了,直接上代码吧!


代码如下:


  
  
  1. #include <iostream>
  2. #include <algorithm>
  3. using namespace std;
  4. int n;
  5. char ch[ 105][ 105];
  6. int main() {
  7. while ( cin >> n) {
  8. // 初始化 (切记 ,必须要初始化!!!)
  9. for ( int i = 0; i < n; i++)
  10. for ( int j = 0; j < n; j++)
  11. ch[i][j] = ' ';
  12. int a = 0;
  13. int b = n;
  14. // 画框
  15. do {
  16. for ( int i = a; i < n - a; i++) { // 画 上下 两条边
  17. ch[a][i] = '*';
  18. ch[n - a - 1][i] = '*';
  19. }
  20. for ( int i = a + 1; i < n - a - 1; i++) { // 画 左右 两条边
  21. ch[i][a] = '*';
  22. ch[i][n - a - 1] = '*';
  23. }
  24. a += 2;
  25. b = b - 4;
  26. } while (b > 0); // 判断是否继续画框的条件
  27. // 输出
  28. for ( int i = 0; i < n; i++) {
  29. for ( int j = 0; j < n; j++) {
  30. cout << ch[i][j];
  31. }
  32. cout << endl;
  33. }
  34. }
  35. return 0;
  36. }

猜你喜欢

转载自blog.csdn.net/shf1730797676/article/details/83683901