728x90
위와 같은 그림을 얻기 위한 코드를 짜보았다.
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
|
#include <iostream>
using namespace std;
int main() {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (i == 0 || i == 8) cout << "#";
else {
if(j==0 || j==8) cout << "#";
else {
if (i == 1 || i == 7) cout << "*";
else if (i == 2 || i == 6) {
if (j != 1 && j != 7) cout << "*";
else cout << " ";
}
else if (i == 3 || i == 5) {
if (j / 3 == 1) cout << "*";
else cout << " ";
}
else {
if (i == 4 && j == 4) cout << "*";
else cout << " ";
}
}
}
}
cout << "\n";
}
}
|
cs |
결과값 출력은 문제없이 되는데
코드가 너무 복잡하고, 가로와 세로가 고정값으로 존재해야지만 가능한 코드이다.
만약 i = 2k + 1, j = 2k + 1로 조건이 주어진다면
코드를 어떻게 고쳐야할까? 해서 생각한 문제
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
#include <iostream>
using namespace std;
int main() {
int n, m; cin >> n >> m;
int l = n;
for (int i = 0; i < n; i++) {
if (i == 0 || i == n - 1) {
for (int j = 0; j < m; j++) cout << "#";
}
else {
int t = (n - l) / 2 - 1;
cout << "#";
for (int j = 0; j < t; j++) cout << " ";
for (int j = 0; j < l; j++) cout << "*";
for (int j = 0; j < t; j++) cout << " ";
cout << "#";
}
i < n / 2 ? l -= 2 : l += 2;
cout << "\n";
}
}
|
cs |
高..
이게 내 실력에서는 최선이다.
물론 결과값도 잘 나오는데
어딘가 많이 아쉽다.
'Study > Algorithm' 카테고리의 다른 글
CC Algorithm (Coin Change) (0) | 2022.12.20 |
---|---|
C++ 조합론 (0) | 2022.12.18 |
nCr 코드 (Combination) (0) | 2022.07.10 |