PAT 乙级 1027

题目

    题目地址:PAT 乙级 1027

 

思路

    本题需要注意两点:

        1. 对于每行输出字符的循环和判断没有完全搞清楚,导致在4 * 的条件下会输出7个字符,n的结果是-3。

        2. 没有考虑到小于等于0的情况,题目给出的数据范围是(<=1000),因此完全可能存在小于等于0的情况,但是在最初没有考虑。

 

代码

 

 1 #include <iostream>
 2 using namespace std;
 3 
 4 int main() {
 5     int n = 0;
 6     char c;
 7     cin >> n >> c;
 8     if (n <= 0) {
 9         return 0;
10     }
11     n--;
12     int low_cnt = 3;
13     while (n > 2 * low_cnt) {
14         if (low_cnt != 1)
15             n -= 2 * low_cnt;
16         else
17             n -= low_cnt;
18         low_cnt += 2;
19     }
20     low_cnt -= 2;
21     int space_cnt = 0;
22     while (low_cnt > 0) {
23         for (int i = 0; i < space_cnt; i++)
24             cout << " ";
25         for (int j = 0; j < low_cnt; j++)
26             cout << c;
27         space_cnt++;
28         low_cnt -= 2;
29         cout << endl;
30     }
31     space_cnt -= 2;
32     low_cnt += 4;
33     while (space_cnt >= 0) {
34         for (int i = 0; i < space_cnt; i++)
35             cout << " ";
36         for (int j = 0; j < low_cnt; j++)
37             cout << c;
38         space_cnt--;
39         low_cnt += 2;
40         cout << endl;
41     }
42     cout << n << endl;
43 
44     return 0;
45 }

 

posted @ 2018-08-15 12:21  moujun  阅读(236)  评论(0编辑  收藏  举报