코딩 테스트/백준

[백준] 알고리즘 단계별로 풀어보기 - 1 (자바)

Nirsa 2022. 3. 6. 15:29
반응형
백준 알고리즘 단계별로 풀어보기 - 1 후기(자바)

하루 1~2시간은 백준 문제 풀려고 합니다!

맨앞부분은 옛날에 파이썬으로 풀었던게 있어서 스킵 했습니다. 이런 문제들을 풀어본 경험이 없어서 그런지, 극 초반부인데도 문제를 읽고 이해하는 과정이 너무 어려웠네요.


10926번: ??!
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String id = sc.next();
		System.out.printf("%s??!",id);
	}
}

 

18108번: 1998년생인 내가 태국에서는 2541년생?!
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		short year = sc.nextShort();
		System.out.printf("%d",(year-543));
	}
}

 

14681번: 사분면 고르기
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int num1 = sc.nextInt();
		int num2 = sc.nextInt();

		if(num1>0 && num2>0) {
			System.out.println("1");
		} else if(num1<0 && num2>0) {
			System.out.println("2");
		} else if(num1<0 && num2<0) {
			System.out.println("3");
		} else if(num1>0 && num2<0) {
			System.out.println("4");
		}
	}
}

 

2884번: 알람 시계
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		short H = sc.nextShort();
		short M = sc.nextShort();
		
		if((0<=H || H<=23) && (0<=M || M<=59)) {
			int alarmM = M-45;
			if(alarmM >= 0) {
				System.out.printf("%d %d", H, alarmM);
			} else if(alarmM < 0 && H > 0) {
				System.out.printf("%d %d", H-1, (60-(-alarmM)));
			} else if(alarmM < 0 && H == 0) {
				System.out.printf("%d %d", 23, (60-(-alarmM)));
			}
		}
	}
}

 

2525번: 오븐 시계
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		short A = sc.nextShort();
		int B = sc.nextShort() + sc.nextShort();
		
		while ( B > 59) {
			A++;
			B -= 60;
			if(A>23) {
				A -= 24;
			}
		}
		System.out.printf("%d %d", A, B);
		
	}
}

 

2480번: 주사위 세개 (틀렸습니다.)

어느 부분에서 틀린건지 모르겠네요.... 다른 문제 풀다가 다시 돌아와서 풀어봐야겠습니다

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		short A = sc.nextShort();
		short B = sc.nextShort();
		short C = sc.nextShort();
		
		if(A==B && B==C) {
			System.out.printf("%d", (10000+(A*1000)));
		} else if(A==B || B==C || A==C) {
			if(A==B || B==C) {
				System.out.printf("%d", (1000+(B*100)));
			} else if(A==C) {
				System.out.printf("%d", (1000+(A*100)));
			}
		} else if(A!=B && B!=C) {
			if(A>B || A>C) {
				System.out.printf("%d", (A*100));
			} else if(B>C || B>A) {
				System.out.printf("%d", (B*100));
			} else if(C>A || C>B) {
				System.out.printf("%d", (C*100));
			}
		}
		
	}
}

 

2739번: 구구단
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		short dan = sc.nextShort();
		
		for(int i=1; i<10; i++) {
				System.out.printf("%d * %d = %d \n", dan, i, dan*i);
		}
	}
}

 

10950번: A+B - 3
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		short T = sc.nextShort();
		
		for(int i=0; i<T; i++) {
			short num1 = sc.nextShort();
			short num2 = sc.nextShort();
			System.out.printf("%d \n", num1+num2);
		}
	}
}

 

8393번: 합
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int sum = 0;
		
		for(int i=1; i<=n; i++) {
			sum = sum + i ;
		}
		System.out.printf("%d \n", sum);
	}
}

 

15552번: 빠른 A + B

BufferedReader , BufferedWriter 사용법을 보며 풀기는 했으나, 어떤식으로 동작하는지 잘 이해가 안가네요. 좀 더 사용법에 대해 알아봐야겠습니다.

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.StringTokenizer;

public class Main {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
		long T = Integer.parseInt(br.readLine());
		
		StringTokenizer st;
		
		for(int i=1; i<=T; i++) {
			st = new StringTokenizer(br.readLine()," ");
			bw.write(Integer.parseInt(st.nextToken()) + Integer.parseInt(st.nextToken())+"\n");
		}
		
		
		br.close();
		bw.flush();
		bw.close();
	}
}

 

반응형