01-09 18:44
Recent Posts
Recent Comments
๊ด€๋ฆฌ ๋ฉ”๋‰ด

miinsun

[Algorithm]์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ž๋ฐ”_16 ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜์—ด ๋ณธ๋ฌธ

Algorithm/Java

[Algorithm]์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ž๋ฐ”_16 ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜์—ด

miinsun 2022. 1. 3. 21:22

 

๐Ÿ’ฌ ๋ฌธ์ œ ์„ค๋ช…

1) ํ”ผ๋ณด๋‚˜ํ‚ค ์ˆ˜์—ด์„ ์ถœ๋ ฅํ•œ๋‹ค. ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜์—ด์ด๋ž€ ์•ž์˜ 2๊ฐœ์˜ ์ˆ˜๋ฅผ ํ•ฉํ•˜์—ฌ ๋‹ค์Œ ์ˆซ์ž๊ฐ€ ๋˜๋Š” ์ˆ˜์—ด์ด๋‹ค.
2) ์ž…๋ ฅ์€ ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜์—ด์˜ ์ด ํ•ญ์˜ ์ˆ˜ ์ด๋‹ค. ๋งŒ์•ฝ 7์ด ์ž…๋ ฅ๋˜๋ฉด 1 1 2 3 5 8 13์„ ์ถœ๋ ฅํ•˜๋ฉด ๋œ๋‹ค.

 

 

๐Ÿ”จ ์ž…์ถœ๋ ฅ ์˜ˆ

์ž…๋ ฅ - ์ฒซ ์ค„์— ์ด ํ•ญ์ˆ˜ N(3<=N<=45)์ด ์ž…๋ ฅ๋œ๋‹ค.

10

์ถœ๋ ฅ - ์ฒซ ์ค„์— ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜์—ด์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

1 1 2 3 5 8 13 21 34 55

 

 

โ€‹

๐Ÿ’ป Solution.java

import java.util.Scanner;
  
public class Main {
	public void solution(int n) {
		int[] answer = new int[n];
		answer[0] = 1;
		answer[1] = 1;
		
		for(int i = 2; i < n; i++) {
			answer[i] = answer[i - 1] + answer[i - 2];
		}
		
		for(int tmp : answer)
			System.out.print(tmp + " ");
	}
	
	public static void main(String[] args){
		Main main  = new Main();
		Scanner sc =new Scanner(System.in);
		int n = sc.nextInt();
		
		main.solution(n);
		
		sc.close();
		return ;
	}
}

 

 

 

Comments