코딩 테스트/백준

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

Nirsa 2022. 3. 7. 21:40
반응형
알고리즘 단계별로 풀어보기 - 2 (자바)

맨처음 브론즈 5였던것 같은데 풀다보니 브론즈 3이 되었네요! 슬슬 벌써 어려워지는데 큰일이네요 ㅠㅠ


2480번: 주사위 세개

지난번에 도저히 눈에 안보였는데... 백준 사이트에서 천사분이 콕 찝어주셨네요!

19, 21, 23 라인의 or 연산이 문제였습니다. and연산으로 바꿔준 후 드디어 해결..ㅠㅠ

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));
			}
		}
		
	}
}

 

2741번: N 찍기
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int input = sc.nextInt();
		int num = 1;
		
		for(int i=0; i<input; i++) {
			System.out.println(num++);
		}
	}
}

 

2742번: 기찍 N
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int input = sc.nextInt();
		int num = input;
		
		for(int i=0; i<input; i++) {
			System.out.println(num--);
		}
	}
}

 

11021번: A+B - 7

이상하게 왜 자꾸 틀렸다고 한건지 이해를 못했었는데, printf를 사용하면 개행 문자까지 해주어야 맞은걸로 처리 되네요.

import java.util.Scanner;

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

 

11022번: A+B - 8
import java.util.Scanner;

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

 

2438번: 별 찍기 - 1
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int T = sc.nextInt();
		
		for(int i=0; i<T; i++) {
			for(int j=0; j<i+1; j++) {
				System.out.printf("*");
			}
			System.out.println();
		}
		
	}
}

 

2439번: 별 찍기 - 2
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int T = sc.nextInt();
		
		for(int i=0; i<T; i++) {
			for(int j=T; j-1>i; j--) {
				System.out.print(" ");
			}
			for(int j=0; j<i+1; j++) {
				System.out.printf("*");
			}
			System.out.println();
		}
	}
}

 

10871번: X보다 작은 수
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int X = sc.nextInt();
		
		int[] A = new int[N];
		for(int i=0; i<A.length; i++) {
			A[i] = sc.nextInt();
			if(A[i] < X) {
				System.out.printf("%d ", A[i]);
			}
		}
	}
}
반응형