Notice
Recent Posts
Recent Comments
Link
250x250
«   2025/08   »
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
Archives
Today
Total
관리 메뉴

피너클의 it공부방

백준 14652 나는 행복합니다~ (c++) : 피너클 본문

백준

백준 14652 나는 행복합니다~ (c++) : 피너클

피너클 2022. 5. 14. 23:26
728x90
반응형

https://www.acmicpc.net/problem/14652

 

14652번: 나는 행복합니다~

첫째 줄에 관중석의 크기를 나타내는 N, M과 잃어버린 관중석 번호를 나타내는 K가 주어진다. (1 ≤ N, M ≤ 30,000, 0 ≤ K ≤ N×M-1)

www.acmicpc.net

단순 구현문제다.

분명 시간초과가 날거라 생각했는데 이유는 모르겠지만 나지 않았다.

cin >> n >> m >> k;
int num = 0;
for (int i = 0; i < n; i++) {
	for (int j = 0; j < m; j++) {
		if (num == k) {
			cout << i << ' ' << j << endl;
			return 0;
		}
		num++;
	}
}

그냥 하나하나씩 확인하며 맞으면 출력하는 방식이다.

하지만 n과 m이 최대 30000이니 시간초과가 날수도 있다.

cout << k / m << ' ' << k % m << endl;

그냥 k를 m으로 나눈 몫과 k를 m으로 나눈 나머지를 출력해도 된다.

#include <iostream>
#include <algorithm>
#include <string>

using namespace std;

int n, m, k;

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

	cin >> n >> m >> k;
	int num = 0;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			if (num == k) {
				cout << i << ' ' << j << endl;
				return 0;
			}
			num++;
		}
	}

}

첫번째 전체 코드다.

#include <iostream>
#include <algorithm>
#include <string>

using namespace std;

int n, m, k;

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

	cin >> n >> m >> k;
	cout << k / m << ' ' << k % m << endl;

}

두번째 전체 코드다.

728x90
반응형
Comments