목록Data Structure & Algorithm/PS - JAVA (270)
Dev.baelanche
x제곱 하기에 수가 너무 크므로 분할하는 방법으로 접근해야 한다. B를 2로 계속 나누어 최대한 작은 수로 만들고 매 연산마다 (A*B)%C = ((A%C)*(B%C))%C 를 이용하여 수를 더 작게 만들어준다. public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); int c = sc.nextInt(); long ans = 1; long mul = a % c; while(b>0) { if(b%2 == 1) { ans *= mul; ans %= c; } mul = ((mul % c) * (mul % c)) %..
규칙에 따라 구현하는 문제이다. 사진틀 정보를 담을 배열은 순서가 보장되고 동적크기를 가진 ArrayList 를 사용했다. 학생 번호와 추천수를 담은 객체를 사용하였고 번호에 따른 정렬을 내부에서 구현했다. public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); ArrayList list = new ArrayList(); for(int i=0; i0) { int rec = sc.nextInt(); boolean st = false; for(int i=0; i
유턴을 하지 않으려면 갈 수 있는 방향이 적어도 2개 이상이어야 한다. 길마다 이동가능한 방향이 2가지 이상인지 확인하면 된다. public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int r = sc.nextInt(); int c = sc.nextInt(); char a[][] = new char[r][c]; for(int i=0; i
입력된 메세지를 알고리즘대로 체크해 새 메세지를 만들어내고 둘을 비교하는 방식으로 풀었다. public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-->0) { String str = sc.next(); char c[] = str.toCharArray(); int alpha[] = new int[26]; StringBuilder sb = new StringBuilder(); for(int i=0; i
단순구현 문제라 정말 막풀었다. 변수 이름이라도 그나마 알아볼수 있게 서술했다;; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s = sc.next(); int len = (s.length() * 5) - (s.length() - 1); drawSideLine(len); drawInnerLine(len); drawMiddleLine(len, s); drawInnerLine(len); drawSideLine(len); } public static void drawSideLine(int len) { int sideLine = 0; for(int i=1; i
입력을 컵홀더를 표시한 형태로 바꾼후 풀었다. 내가 짠 코드대로 하게되면 일반좌석만 있을경우에 출력이 오답이 나와서 그 부분만 예외처리했다. public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); String s = sc.next(); String s2 = s; s2 = s2.replaceAll("S", "*S*"); s2 = s2.replaceAll("LL", "*LL*"); s2 = s2.replaceAll("\\*\\*", "*"); int cnt = 0; for(int i=0; i
배열에 폭죽이 터질때의 시간을 저장한다. 폭죽이 동시에 터진다면 1개만 카운트한다. public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int c = sc.nextInt(); int a[] = new int[c+1]; int cnt = 0; for(int i=0; i
차례대로 A AND B, A OR B, A XOR B, NOT A, NOT B 를 출력하면 된다. public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); char a[] = sc.next().toCharArray(); char b[] = sc.next().toCharArray(); for(int i=0; i