※ 글쓴이는 취미로 코딩을 익혀보는 사람이라 정확하지 않은 내용을 담고 있을 수 있다 ※

 

이번에 볼 문제는 백준 6030번 문제인 Scavenger Hunt이다.
문제는 아래 링크를 확인하자.

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

 

6030번: Scavenger Hunt

Farmer John has scattered treats for Bessie at special places in the pasture.  Since everyone knows that smart cows make tasty milk, FJ has placed the treats at locations that require Bessie to think. He has given her two numbers, P and Q (1 <= P <= 6,000

www.acmicpc.net

P의 약수와 Q의 약수들을 미리 따로 구하고, 가능한 순서쌍들을 사전순에 따라서 출력해주자.

 

위와 같은 전처리를 하지 않고 p와 q를 각각 1부터 P, Q까지 살피는 단순 반복문을 이용하더라도 P와 Q의 제한이 작아 문제를 해결할 수 있다.

 

아래는 제출한 소스코드이다.

#include <iostream>
#include <vector>
using namespace std;

vector<int> vecP;
vector<int> vecQ;

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

	int P, Q; cin >> P >> Q;
	for (int i = 1; i <= P; i++) {
		if (P % i == 0) vecP.emplace_back(i);
	}
	for (int i = 1; i <= Q; i++) {
		if (Q % i == 0) vecQ.emplace_back(i);
	}
	for (auto p : vecP) {
		for (auto q : vecQ) {
			cout << p << ' ' << q << '\n';
		}
	}
}
728x90

+ Recent posts