1 | 1003 | ํผ๋ณด๋์น ํจ์ |
๋จ์ ์ฌ๊ท๋ก ํผ๋ณด๋์น ์๋ฅผ ๊ตฌํ๋ฉด ์ ๋๋ฆด๊น์? ํจ์ ํธ์ถ์ ๊ฐ์๊ฐ ๊ธฐํ๊ธ์์ ์ผ๋ก ๋์ด๋๊ธฐ ๋๋ฌธ์ ๋๋ค |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader bfr = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(bfr.readLine());
for(int i=0; i<T; i++) {
int num = Integer.parseInt(bfr.readLine());
int zero = 1;
int one = 0;
int temp;
for(int j=0; j<num; j++) {
temp = one;
one = zero + one;
zero = temp;
}
System.out.println(zero + " " + one);
}
}
}
2 | 9184 | ์ ๋๋ ํจ์ ์คํ |
์ฌ๊ท ํธ์ถ๋ง ์๊ฐํ๋ฉด ์ ์ด ๋๋ค! ์๋๊ฐ์? |
import java.util.Scanner;
public class Main {
static int array[][][] = new int[51][51][51];
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int a=0, b=0, c=0;
while(true){
a = scan.nextInt();
b = scan.nextInt();
c = scan.nextInt();
if(a==-1 && b==-1 && c==-1) break;
System.out.printf("w(%d, %d, %d) = %d\n",a,b,c,w(a,b,c));
}
}
static int w(int a, int b, int c) {
if(a<=0 || b<=0 || c<=0)
return 1;
else if(array[a][b][c] != 0)
return array[a][b][c];
else if(a>20 || b>20 || c>20)
return array[a][b][c] = w(20,20,20);
else if(a<b && b<c)
return array[a][b][c] = w(a,b, c-1) + w(a, b-1, c-1) - w(a, b-1, c);
else
return array[a][b][c] = w(a-1, b, c) + w(a-1, b-1, c) + w(a-1, b, c-1) - w(a-1, b-1, c-1);
}
}
3 | 1904 | 01ํ์ผ |
์ ํ์์ ๊ฐ์ ํน์ ์์๋ก ๋๋ ๋๋จธ์ง๋ฅผ ๊ตฌํ๋ ๋ฌธ์ |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
static int[] array = new int [1000001];
public static void main(String[] args) throws IOException {
BufferedReader bfr = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(bfr.readLine());
array[0] = 1;
array[1] = 2;
array[2] = 3;
for(int i=3; i<=N; i++) {
array[i] = array[i-1] + array[i-2];
array[i] %= 15746;
}
System.out.println(array[N-1]);
}
}
4 | 9461 | ํ๋๋ฐ ์์ด |
ํผ๋ณด๋์น ์์ ๋น์ทํ ๊ท์น์ ์ฐพ์ ๋์ ๊ณํ๋ฒ์ผ๋ก ํธ๋ ๋ฌธ์ |
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int T = scan.nextInt();
for(int i =0; i<T; i++) {
int a = scan.nextInt();
if(a==1 || a==2 || a==3) {
System.out.println(1);
continue;
}
Long array[] = new Long[a];
array[0] =(long) 1;
array[1] =(long)1;
array[2] =(long)1;
for(int j=3; j<a; j++) {
array[j] = array[j-3] + array[j-2];
}
System.out.println(array[a-1]);
}
}
}
5 | 1149 | RGB๊ฑฐ๋ฆฌ |
i๋ฒ์งธ ์ง์ ๊ฐ๊ฐ์ ์์ผ๋ก ์น ํ ๋, 1~i๋ฒ์งธ ์ง์ ๋ชจ๋ ์น ํ๋ ์ต์ ๋น์ฉ์ผ๋ก ๋ถ๋ถ๋ฌธ์ ๋ฅผ ์ ์ํด๋ด ์๋ค. |
import java.util.Scanner;
public class Main {
static int T;
static int array[][], dp[][];
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
T = scan.nextInt();
//๋ฐฐ์ด๊ฐ ์ ์ฅ.
array = new int[T][T];
for(int i=0; i<T; i++) {
for(int j=0; j<=i; j++) {
array[i][j] = scan.nextInt();
}
}
//๊น์ดํ์ํ ๋ฐฐ์ด ์์ฑ
dp =new int [T][T];
for(int i=0; i<T; i++)
dp[T-1][i] = array[T-1][i];
//๋งจ ์๋์ธต ๊ฐ๋ง ์ ์ฅ
System.out.println(test(0,0));
}
static int test(int depth, int index) {
if(depth == T-1) return dp[depth][index];
if(dp[depth][index] == 0) {
dp[depth][index] = Math.max(test(depth+1, index), test(depth+1, index+1)) + array[depth][index];
}
return dp[depth][index];
}
}
6 | 1932 | ์ ์ ์ผ๊ฐํ |
๊ฐ ์ธต์ ๋ชจ๋ ์นธ๋ง๋ค ์ต๋๊ฐ์ ์ ์ฅํ๋ฉด์ ๋์ ๊ณํ๋ฒ์ผ๋ก ํธ๋ ๋ฌธ์ |
import java.util.Scanner;
public class Main {
static int T;
static int array[][], dp[][];
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
T = scan.nextInt();
//๋ฐฐ์ด๊ฐ ์ ์ฅ.
array = new int[T][T];
for(int i=0; i<T; i++) {
for(int j=0; j<=i; j++) {
array[i][j] = scan.nextInt();
}
}
//๊น์ดํ์ํ ๋ฐฐ์ด ์์ฑ
dp =new int [T][T];
for(int i=0; i<T; i++)
dp[T-1][i] = array[T-1][i];
//๋งจ ์๋์ธต ๊ฐ๋ง ์ ์ฅ
System.out.println(test(0,0));
}
static int test(int depth, int index) {
if(depth == T-1) return dp[depth][index];
if(dp[depth][index] == 0) {
dp[depth][index] = Math.max(test(depth+1, index), test(depth+1, index+1)) + array[depth][index];
}
return dp[depth][index];
}
}
7 | 2579 | ๊ณ๋จ ์ค๋ฅด๊ธฐ |
i๋ฒ์งธ ๊ณ๋จ์ ์ค๋ฅผ ๋, ๋ช ๊ฐ์ ์ฐ์ํ ๊ณ๋จ์ ์ฌ๋๋์ง๋ฅผ ๊ณ ๋ คํ์ฌ ๋ถ๋ถ๋ฌธ์ ๋ฅผ ์ ์ํด๋ด ์๋ค. |
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
//์
๋ ฅ๊ฐ ์
๋ ฅ๋ฐ๊ธฐ
int T = scan.nextInt();
int array[] =new int [T+1];
int dp[]= new int [T+1];
for(int i=1; i<=T; i++)
array[i] = scan.nextInt();
dp[1] = array[1];
if(T>=2) dp[2] = array[2]+dp[1];
for(int i=3; i<=T; i++) {
dp[i] = Math.max((array[i] + array[i-1] + dp[i-3]), (array[i] + dp[i-2]));
}
System.out.println(dp[T]);
}
}
8 | 1463 | 1๋ก ๋ง๋ค๊ธฐ |
๋ฉ๋ชจ์ด์ ์ด์ ์ผ๋ก N์ 1๋ก ๋ฐ๊พธ๊ธฐ ์ํด ์ฃผ์ด์ง ์ฐ์ฐ์ ๋ช ๋ฒ ์ฌ์ฉํ๋์ง ๊ณ์ฐํ๋ ๋ฌธ์ |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException{
BufferedReader bfr = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(bfr.readLine());
int dp[] = new int[n+1];
//n์ด 0์ด๊ฑฐ๋ 1์ด๋ฉด ์ต์ํ์=0์
dp[0] = dp[1] = 0;
//n์ด 2์ด์์ด๋ฉด ์
์ค์ ๊ฐ์ฅ ์์๊ฐ.
for(int i=2; i<=n; i++) {
dp[i] = dp[i-1]+1;
if(i%3==0) dp[i] = Math.min(dp[i], dp[i/3]+1);
if(i%2==0) dp[i] = Math.min(dp[i], dp[i/2]+1);
}
System.out.println(dp[n]);
}
}
728x90
๋ฐ์ํ