-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1027. 打印沙漏(20).cpp
86 lines (75 loc) · 1.24 KB
/
1027. 打印沙漏(20).cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
#include <string>
using namespace std;
int main()
{
int n;
char c;
cin >> n >> c;
if (n <= 6)
{
cout << c << endl;
cout << n - 1;
}
else
{
//先判断有几层
//x为最大的星星行是几个星星
int x = 1;
//count是一共多少个星星
int count = 1;
//上层行数
int h = 0;
int realCount = 1;
while (count <= n) {
h++;
x += 2;
realCount = count;
count = count + (x + 2) * 2;
}
//cout << "x is " << x << " h is " << h << " realCount is " << realCount;
//上半层
//星星前面空格数
int kgs = 0;
//i代表每一行的星星数
int i = x;
while (i >= 3) {
int xnkgs = kgs;
while(xnkgs--) {
cout << " ";
}
kgs++;
int m = i;
while(m--) {
cout << c;
}
cout << endl;
i = i - 2;
}
//中间的星星
int xnh = h;
while(xnh--) {
cout << " ";
}
cout << c << endl;
//下半层
int xxnh = h;
int q = 3;
int xxxnh = h;
while(xxnh--) {
xxxnh--;
int mh = xxxnh;
while(mh--) {
cout << " ";
}
for(int i = 1; i <= q; i++) {
cout << c;
}
cout << endl;
q += 2;
}
int rrr;
rrr = 2 * h * (h + 2) + 1;
cout << n - rrr;
}
}