문제 : https://www.acmicpc.net/problem/2523


[알고리즘풀이]

위, 아래로 나눠서 따로 구현하면 된다.

#include<iostream>

using namespace std;

int main(void) {
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++){
		for (int j = 0; j < i; j++)
			cout << '*';
		cout << '\n';
	}
	for (int i = n - 1; i > 0; i--){
		for (int j = 0; j < i; j++)
			cout << '*';
		cout << '\n';
	}
}

 

문제 : https://www.acmicpc.net/problem/2522


[알고리즘풀이]

반으로 나눠 위, 아래를 따로 구현한다.

#include<iostream>

using namespace std;

int main(void) {
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++) {
		for (int j = n - i; j > 0; j--)
			cout << ' ';
		for (int j = 1; j <= i; j++)
			cout << '*';
		cout << '\n';
	}
	for (int i = 1; i < n; i++) {
		for(int j = 0; j < i; j++)
			cout << ' ';
		for (int j = 0; j < n - i; j++)
			cout << '*';
		cout << '\n';
	}
	return 0;

}

 

'Problem Solving > BOJ' 카테고리의 다른 글

[BOJ] 2556 : '별 찍기 - 14' travelbeeee  (0) 2019.10.26
[BOJ] 2523 : '별 찍기 - 13' travelbeeee  (0) 2019.10.26
[BOJ] 2447 - '별 찍기 - 10'  (0) 2019.10.26
[BOJ] 2446 - '별 찍기 - 9'  (0) 2019.10.26
[BOJ] 2445 - '별 찍기 - 8'  (0) 2019.10.26

문제 : https://www.acmicpc.net/problem/2447


[알고리즘풀이]

n = 3, n = 9, n = 27일 때의 그림들을 보며 규칙을 유추할 수 있다.

1. x 와 y 좌표를 3씩 나눠가면 x % 3 == 1 && y % 3 == 1 인 순간이 나오면, *을 출력한다.

2. 1이 아니라면 공백을 출력한다.

#include<iostream>

using namespace std;

int main() { 
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);

	int n;
	cin >> n;

	for (int i = 0; i < n; i++) { 
		for (int j = 0; j < n; j++) {
			int x = i, y = j;
			bool flag = false;
			while (x) {
				if (x % 3 == 1 && y % 3 == 1) {
					flag = true;
				}
				x /= 3, y /= 3;
			}
			if (!flag)
				cout << '*';
			else
				cout << ' ';
		}
		cout << '\n';
	} 
	return 0; 
}


 

'Problem Solving > BOJ' 카테고리의 다른 글

[BOJ] 2523 : '별 찍기 - 13' travelbeeee  (0) 2019.10.26
[BOJ] 2522 : '별 찍기-12' travelbeeee  (0) 2019.10.26
[BOJ] 2446 - '별 찍기 - 9'  (0) 2019.10.26
[BOJ] 2445 - '별 찍기 - 8'  (0) 2019.10.26
[BOJ] 2444 - '별 찍기 -7'  (0) 2019.10.26

문제 : https://www.acmicpc.net/problem/2446


[알고리즘풀이]

위와 아래로 나눠서 구현합니다.

#include<iostream>

using namespace std;


int main(void) {
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);

	int n;
	cin >> n;
	// 역삼각형
	for (int i = 1; i <= n; i++) {
		// 공백출력
		for (int j = 0; j < i - 1; j++)
			cout << ' ';
		for (int j = 0; j < (2 * n - 1) - 2 * (i - 1); j++)
			cout << '*';
		cout << '\n';
	}
	// 나머지부분
	for (int i = 1; i < n; i++) {
		// 공백 출력
		for (int j = 0; j < (n - 1) - i; j++)
			cout << ' ';
		for (int j = 0; j < 2 * i + 1; j++)
			cout << '*';
		cout << '\n';
	}
}

 

'Problem Solving > BOJ' 카테고리의 다른 글

[BOJ] 2522 : '별 찍기-12' travelbeeee  (0) 2019.10.26
[BOJ] 2447 - '별 찍기 - 10'  (0) 2019.10.26
[BOJ] 2445 - '별 찍기 - 8'  (0) 2019.10.26
[BOJ] 2444 - '별 찍기 -7'  (0) 2019.10.26
[BOJ] 2443 - '별 찍기 - 6'  (0) 2019.10.26

+ Recent posts