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

 

이번에 볼 문제는 백준 11366번 문제인 Tons of Orcs, no Fibbin'이다.
문제는 아래 링크를 확인하자.

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

 

11366번: Tons of Orcs, no Fibbin’

The armies of Mordor are fearsome in both stature and numbers. How did they raise such a host in so short a time? It turns out, orcs breed very quickly. For any given year, their population equals the sum of the populations from the previous two years. For

www.acmicpc.net

주어지는 세 수를 입력받아 피보나치 점화식을 이용하여 K회 다음의 수를 계산해내는 문제이다.

 

대부분의 상황에서 이 수열은 지수적으로 증가하므로 K의 횟수가 크지 않다

 

예외적으로 오크가 한 마리도 없는 상황의 경우 K가 몇이 되더라도 항상 값이 0이 나오게 된다. 이를 예외처리하지 않을 경우 루프를 K회 돌아 시간초과가 나게 된다.

 

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

#include <iostream>
using namespace std;

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

	int x, y, K;
	
	while (cin >> x >> y >> K) {
		if (K == 0) break;

		if (x == 0 && y == 0) cout << 0 << '\n';
		else {
			while (K--) {
				int tmp = x + y;
				x = y;
				y = tmp;
			}
			cout << y << '\n';
		}
	}
}
728x90

+ Recent posts