본문 바로가기
Algorithm/Baekjoon

Baekjoon 15841 Virus Outbreak JAVA

by Hunveloper 2022. 6. 17.
728x90

 

15841번: Virus Outbreak

For each input value, the output contains a line in the format: Hour X: Y cow(s) affected, where X is the hour, and Y is the total affected cows that need to be euthanized based on the hour given by X.

www.acmicpc.net

문제

The State Veterinary Services Department recently reported an outbreak of a newly found cow disease. All cows found to have affected by the disease have since euthanized because of the risk to the industry. Number of affected cows increased to 21, 34 and reached 55 after eight, nine and ten hours respectively.

You have been assigned by the authority to study the pattern of the outbreak and develop a program to predict the next number of affected cows so that the authorities could prepare and act accordingly.

입력

Input will consist of a series of positive integer numbers no greater than 490 each on a separate line represent the hours. The input process will be terminated by a line containing -1.

출력

For each input value, the output contains a line in the format: Hour X: Y cow(s) affected, where X is the hour, and Y is the total affected cows that need to be euthanized based on the hour given by X.

풀이

주어진 입력을 가지고, 규칙을 찾아서 피보나치 수열을 만드는 문제이다.

코드
import java.io.*;
import java.math.BigInteger;

public class Main {
	public static void main(String[] args) throws Exception{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
		int n=0;
		BigInteger [] arr = new BigInteger[500];
		arr[0]=BigInteger.ZERO;
		arr[1]=BigInteger.ONE;
		for(int i=2;i<=490;i++)
			arr[i]=arr[i-2].add(arr[i-1]);
		while((n=Integer.parseInt(br.readLine()))!=-1) {
			bw.write("Hour "+n+": "+arr[n]+" cow(s) affected\n");
		}
		bw.close();
	}
}

 

728x90
728x90

'Algorithm > Baekjoon' 카테고리의 다른 글

Baekjoon 1189 컴백홈 JAVA  (0) 2022.06.17
Baekjoon 1059 좋은 구간 JAVA  (0) 2022.06.17
Baekjoon 14888 연산자 끼워넣기 JAVA  (0) 2022.06.15
Baekjoon 1343 폴리오미노 JAVA  (0) 2022.06.15
Baekjoon 2018 수들의 합 5 JAVA  (0) 2022.06.10

댓글