https://www.acmicpc.net/problem/10988

 

입력받은 문자열을 뒤집어도 기존과 동일한지 여부를 확인하면 된다.

 

문자열 앞·뒤 에서부터 String.charAt() 메소드를 이용하여 확인해도 되지만

StringBuilder.reverse() 메소드를 이용하여 문자열을 뒤집은 뒤 String.equals()로 풀었다.

import java.util.*;
 
 public class Main {
 	public static void main(String[] args) {
 		Scanner scanner = new Scanner(System.in);
 		String str = scanner.next();
 		
 		StringBuilder sb = new StringBuilder(str);
 		String reverseStr = sb.reverse().toString();
 
 		System.out.print(str.equals(reverseStr) ? 1 : 0);
 	}
 }

https://www.acmicpc.net/problem/2444

 

보자마자 한번에 풀 정도의 두뇌가 아니기에...

몇번의 노가다 끝에 푸는 그닥 좋아하는 문제는 아니다.

 

가운데 2 X N - 1 개수의 별을 기준으로

앞 뒤로 피라미드 별을 찍는 두 개의 반복문을 두어 해결하였다.

import java.util.Scanner;

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

		for (int i = 1; i < N; i++) {
			for (int j = N - i; j > 0; j--) System.out.print(" ");
			for (int j = 0; j < i * 2 - 1; j++) System.out.print("*");
			System.out.println();
		}
		
		for (int i = 0; i < N * 2 - 1; i++) System.out.print("*");
		System.out.println();

		for (int i = N - 1; i > 0; i--) {
			for (int j = N - i; j > 0; j--) System.out.print(" ");
			for (int j = 0; j < i * 2 - 1; j++) System.out.print("*");
			System.out.println();
		}
	}
}

https://www.acmicpc.net/problem/3003

 

문제에 값과 개수가 주어져 크게 난해하지 않은 문제이다.

주어진 체스말을 배열로 선언 후 사용자 입력 값을 빼며 계산하였다.

import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int[] chess = {1, 1, 2, 2, 2, 8};
		int[] result = new int[6];

		for (int i = 0; i < chess.length; i++) {
			int a = scanner.nextInt();
			result[i] = chess[i] - a;
		}

		for (int i : result) {
			System.out.print(i + " ");
		}
	}
}

'Algorithm' 카테고리의 다른 글

[백준] 10988. 팰린드롬인지 확인하기  (1) 2025.03.15
[백준] 2444. 별 찍기 - 7  (0) 2025.03.15
[백준] 27866. 문자와 문자열  (1) 2025.03.14
[백준] 10811. 바구니 뒤집기  (0) 2025.03.13
[백준] 27433. 팩토리얼 2  (1) 2025.03.07

https://www.acmicpc.net/problem/27866

String.charAt() 메소드를 활용하여 해결하였다.

charAt 메소드는 배열처럼 첫 글자를 0번으로 취급하니 charAt(i - 1)로 풀어야한다.

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		
		String S = scanner.nextLine();
		int i = scanner.nextInt();

		System.out.println(S.charAt(i - 1));
	}
}

https://www.acmicpc.net/problem/10811

 

배열을 활용한 문제로 잘 해석하며 로직을 생각하다보면 풀리는 문제였던것 같다.

 

배열의 항목들을 바꾸는 과정에서의 반복문의 i 변수를 더하고 빼는 로직이 헷갈릴 수 있으니 주의하자.

import java.util.*;
 import java.util.stream.IntStream;
 
 public class Main {
 	public static void main(String args[]) {
 		Scanner scanner = new Scanner(System.in);
 		
 		int n = scanner.nextInt();
 		int m = scanner.nextInt();
 		int[][] reverse = new int[m][2];
 
 		for (int i = 0; i < m; i++) {
 			reverse[i][0] = scanner.nextInt();
 			reverse[i][1] = scanner.nextInt();
 		}
 		
 		solution(n, m, reverse);
 	}
 
 	public static void solution(int n, int m, int[][] reverse) {
 		int[] arr = IntStream.rangeClosed(1, n).toArray();
 
 		for (int k = 0; k < m; k++) {
 			int i = reverse[k][0];
 			int j = reverse[k][1];
 
 			arr = reverseArray(i, j, arr);
 		}
 
 		for (int num : arr) {
 			System.out.print(num + " ");
 		}
 	}
 
 	public static int[] reverseArray(int start, int end, int[] array) {
 		for (int i = 0; i < (end - start) / 2 + 1; i++) {
 			int temp = array[start - 1 + i];
 			array[start - 1 + i] = array[end - 1 - i];
 			array[end - 1 - i] = temp;
 		}
 		
 		return array;
 	}
 }

문제: https://www.acmicpc.net/problem/27433

 

맨 처음 재귀를 생각하지 못하고 for문을 이용한 단순 반복문으로 풀었다.

단순 반복문

import java.util.*;

public class Main {
	public static void main(String args[]) {
		Scanner scanner = new Scanner(System.in);
		int n = scanner.nextInt();
		
		System.out.println(solution(n));
	}

	public static long solution(int n) {
		long result = 1;
		
		if (n == 0) return 1;
		
		for (int i = n; i > 0; i--) {
			result = result * i;
		}

		return result;
	}
}

 

추후 재귀를 활용하여 다시 풀어보았다.

재귀함수

import java.util.*;

public class Main {
	public static void main(String args[]) {
		Scanner scanner = new Scanner(System.in);
		int n = scanner.nextInt();
		
		System.out.println(solution(n));
	}

	public static long solution(int n) {
		if (n == 0) return 1;
		else return n * solution(n-1);
	}
}

 

문제의 조건이 0 ≤ N ≤ 20 이기에 long으로도 해결했다면, 조건이 없을 경우 BigInteger 클래스를 활용하면 된다.

BigInteger

import java.util.*;
import java.math.*;

public class Main {
	public static void main(String args[]) {
		Scanner scanner = new Scanner(System.in);
		int n = scanner.nextInt();
		
		System.out.println(solution(n));
	}

	public static BigInteger solution(int n) {
		if (n == 0) return BigInteger.ONE;
		else return BigInteger.valueOf(n).multiply(solution(n - 1));
	}
}

+ Recent posts