문제

 

 

풀이

k층의 n호에는 k층 n-1호에 사는 사람 수와 k-1층 n호에 사는 사람 수를 더한 만큼 살고 있다.

#include <iostream>

using namespace std;

int func(int k, int n) {
	if (n == 1) return 1;
	if (k == 0) return n;

	return (func(k, n - 1) + func(k - 1, n));
}

int main() {	
	int t, k, n;

	cin >> t;

	for (int i = 0; i < t; i++) {
		cin >> k >> n;
		
		cout << func(k, n) << "\n";
	}

	return 0;
}

'백준 > 기타' 카테고리의 다른 글

7568번: 덩치 [C++]  (0) 2023.04.06
11050번: 이항 계수 1 [C++]  (0) 2023.04.06
2609번: 최대공약수와 최소공배수 [C++]  (0) 2023.04.06
1259번: 팰린드롬수 [C++]  (0) 2023.04.06
15829번: Hashing [C++]  (0) 2023.04.06

문제

 

 

풀이

유클리드 호제법을 사용해 최대 공약수와 최소 공배수를 구하였다.

#include <iostream>

using namespace std;

int gcd(int a, int b) {
	if (a % b == 0) return b;
	else return gcd(b, a % b);
}

int main() {
	int n, m;
	int gt, lt;

	cin >> n >> m;

	if (n >= m) {
		gt = n;
		lt = m;
	}
	else {
		gt = m;
		lt = n;
	}

	cout << gcd(gt, lt) << "\n" << (n * m) / gcd(gt, lt);

	return 0;
}

'백준 > 기타' 카테고리의 다른 글

11050번: 이항 계수 1 [C++]  (0) 2023.04.06
2775번: 부녀회장이 될테야 [C++]  (0) 2023.04.06
1259번: 팰린드롬수 [C++]  (0) 2023.04.06
15829번: Hashing [C++]  (0) 2023.04.06
4153번: 직각삼각형 [C++]  (0) 2023.04.06

문제

 

 

풀이

문자열을 뒤집어서 뒤집기 전 문자열과 동일한지 판별하면 된다.

#include <iostream>

using namespace std;

int main() {
	string s1, s2;

	while (true) {
		cin >> s1;

		if (s1 == "0") break;

		for (int i = 0; i < s1.length(); i++)
			s2 += s1[s1.length() - i - 1];

		if (s1 == s2) cout << "yes\n";
		else cout << "no\n";

		s2.clear();
	}

	return 0;
}

문제

 

 

풀이

 주어진 식대로 계산하면 된다.

#include <iostream>
#include <string>

using namespace std;

int main() {
	int n;
	long long hash = 0, r = 1, m = 1234567891;
	string str;

	cin >> n >> str;

	for (int i = 0; i < str.length(); i++) {
		hash = (hash + (str[i] - 'a' + 1) * r) % m;

		r = (r *31) % m;
	}

	cout << hash;

	return 0;
}

문제

 

 

풀이

세 정수를 받아 피타고라스의 정리를 이용해 직각삼각형인지 판별하고, 0 0 0이 입력되면 반복문이 종료되도록 한다.

#include <iostream>

using namespace std;

int main() {
	int x, y, z;
	int tmp;

	while (true) {
		cin >> x >> y >> z;

		if ((x == 0) && (y == 0) && (z == 0)) break;

		if (x > y) {
			tmp = y;
			y = x;
			x = tmp;
		}

		if (y > z) {
			tmp = z;
			z = y;
			y = tmp;
		}

		if (x * x + y * y == z * z) cout << "right\n";
		else cout << "wrong\n";
	}

	return 0;
}

'백준 > 기타' 카테고리의 다른 글

2609번: 최대공약수와 최소공배수 [C++]  (0) 2023.04.06
1259번: 팰린드롬수 [C++]  (0) 2023.04.06
15829번: Hashing [C++]  (0) 2023.04.06
2903번: 중앙 이동 알고리즘 [C++]  (0) 2023.04.03
2720번: 세탁소 사장 동혁 [C++]  (0) 2023.04.03

문제

 

 

풀이

주어진 조건대로 n번 거쳤을 때 점의 총 개수는 아래 표와 같다.

n = 0 n = 1 n = 2 n = 3 n = 4 ...
2 x 2 3 x 3 5 x 5 9 x 9 17 x 17 ...

위 표를 통해 (1 + 2n) x (1 + 2n)의 규칙이 있음을 알 수 있다.

#include <iostream>
#include <string>

using namespace std;

int main() {
	int n;
	int num = 1;

	cin >> n;

	num <<= n;

	cout << (1 + num) * (1 + num);

	return 0;
}

'백준 > 기타' 카테고리의 다른 글

2609번: 최대공약수와 최소공배수 [C++]  (0) 2023.04.06
1259번: 팰린드롬수 [C++]  (0) 2023.04.06
15829번: Hashing [C++]  (0) 2023.04.06
4153번: 직각삼각형 [C++]  (0) 2023.04.06
2720번: 세탁소 사장 동혁 [C++]  (0) 2023.04.03

문제

 

 

풀이

#include <iostream>
#include <string>

using namespace std;

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

	int t;

	cin >> t;

	for (int i = 0; i < t; i++) {
		int c;
		int quarter = 0, dime = 0, nickel = 0, penny = 0;

		cin >> c;

		quarter = c / 25;
		c %= 25;

		dime = c / 10;
		c %= 10;

		nickel = c / 5;
		c %= 5;

		penny = c / 1;
		c %= 1;

		cout << quarter << ' ' << dime << ' ' << nickel << ' ' << penny << '\n';
	}

	return 0;
}

'백준 > 기타' 카테고리의 다른 글

2609번: 최대공약수와 최소공배수 [C++]  (0) 2023.04.06
1259번: 팰린드롬수 [C++]  (0) 2023.04.06
15829번: Hashing [C++]  (0) 2023.04.06
4153번: 직각삼각형 [C++]  (0) 2023.04.06
2903번: 중앙 이동 알고리즘 [C++]  (0) 2023.04.03

+ Recent posts