본문 바로가기
Algorithm/백준(BOJ)

백준 5단계 -Java

by k-mozzi 2023. 4. 18.
반응형
Preface

 

5단계는 문자열과 관련된 문제들이었는데, 며칠 전 이것이 자바다 책에서 다양한 문자열 메소드를 공부한 탓인지 큰 어려움 없이 문제들을 풀 수 있었다.

 

2908번을 해결한 후 다른 사람들의 코드를 찾아보던 중 StringBuilder 객체를 생성할 때 String 타입의 값은 append( ) 메소드를 사용할 필요 없이 StringBuilder 객체를 생성하면서 값을 할당할 수 있다는 것을 알게 되었다.


 

https://www.acmicpc.net/step/7

 

문자열 단계

정수를 문자열로 입력받는 문제. Python처럼 정수 크기에 제한이 없다면 상관 없으나, 예제 3은 일반적인 정수 자료형에 담기에 너무 크다는 점에 주목합시다.

www.acmicpc.net

 

 

- 27866번

package num5;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class B27866 {

	public static void main(String[] args) throws IOException {

		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String s = br.readLine();
		int i = Integer.parseInt(br.readLine());

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

}

 

 

- 2743번

package num5;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class B2743 {

	public static void main(String[] args) throws IOException {

		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String s = br.readLine();
		System.out.println(s.length());
	}

}

 

 

- 9086번

package num5;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class B9086 {

	public static void main(String[] args) throws IOException {

		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int num = Integer.parseInt(br.readLine());
		String[] arr = new String[num];	// StringBuilder을 사용하면 더 간결하게 작성 가능
		for (int i = 0; i < num; i++) {
			arr[i] = br.readLine();
		}

		for (int j = 0; j < num; j++) {
			int c = arr[j].length();
			System.out.println("" + arr[j].charAt(0) + arr[j].charAt(c-1));
		}
	}

}

 

 

- 11654번

package num5;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class B11654 {

	public static void main(String[] args) throws IOException {

		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int ch = br.readLine().charAt(0);
		System.out.println(ch);
		// charAt() 메소드로 얻은 값(char)이 int 형으로 캐스팅 됨
		// charAt()의 리턴 타입은 char이다.
		// charAt()은 해당 문자의 아스키코드 값을 반환하므로 반드시 -48 또는 -'0'을 해야 원하는 정수 값을 얻을 수 있다. 
		// charAt()은 해당 문자의 아스키코드 값을 반환하므로 반드시 -97 또는 -'a'를 해야 원하는 문자를 얻을 수 있다. 

//		int a = System.in.read();	byte 값으로 문자 하나를 읽으며 문자에 해당하는 아스키코드 값을 저장
//		System.out.print(a);
	}

}

→ 참고 블로그: https://st-lab.tistory.com/59

 

[백준] 11654번 : 아스키 코드 - JAVA [자바]

https://www.acmicpc.net/problem/11654 11654번: 아스키 코드 알파벳 소문자, 대문자, 숫자 0-9중 하나가 주어졌을 때, 주어진 글자의 아스키 코드값을 출력하는 프로그램을 작성하시오. www.acmicpc.net 문제 매

st-lab.tistory.com

 

 

- 11720번

package num5;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class B11720 {

	public static void main(String[] args) throws IOException {

		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int N = Integer.parseInt(br.readLine());
		String realN = br.readLine();
		int sum = 0;
		
		for (int i = 0; i < N; i++) {
			int c = Integer.parseInt(realN.substring(i, i+1));
			
			sum += c;
		}
		
		System.out.println(sum);
	}

}

 

 

- 10809번

package num5;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class B10809 {

	public static void main(String[] args) throws IOException {

		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String S = br.readLine();
		br.close();
		String alpha = "abcdefghijklmnopqrstuvwxyz";

		for (int i = 0; i < alpha.length(); i++) {
			String c = alpha.substring(i, i + 1);
			if (S.indexOf(c) != -1) {
				System.out.print(S.indexOf(c) + " ");
			} else {
				System.out.print(-1 + " ");
			}
		}
	}

}


//public class Main {
//	 
//	public static void main(String[] args) throws IOException {
//		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// 
//		int[] arr = new int[26];
//		
//		for(int i = 0; i < arr.length; i++) {
//			arr[i] = -1;
//		}
// 
//		String S = br.readLine();
// 
//		for(int i = 0; i < S.length(); i++) {
//			char ch = S.charAt(i);
//    
//			if(arr[ch - 'a'] == -1) {	// arr 원소 값이 -1 인 경우에만 초기화
//				arr[ch - 'a'] = i;
//			}
//		}
// 
//		for(int val : arr) {	// 배열 출력
//			System.out.print(val + " ");
//		}
//	}
//}

→ 주석 코드 참고 블로그: https://st-lab.tistory.com/62

 

[백준] 10809번 : 알파벳 찾기 - JAVA [자바]

https://www.acmicpc.net/problem/10809 10809번: 알파벳 찾기 각각의 알파벳에 대해서, a가 처음 등장하는 위치, b가 처음 등장하는 위치, ... z가 처음 등장하는 위치를 공백으로 구분해서 출력한다. 만약, 어

st-lab.tistory.com

 

 

- 2675번

package num5;

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

public class B2675 {

	public static void main(String[] args) throws IOException {

		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int T = Integer.parseInt(br.readLine());
		StringTokenizer st;
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < T; i++) {
			st = new StringTokenizer(br.readLine());
			int R = Integer.parseInt(st.nextToken());
			String S = st.nextToken();
			for (int j = 0; j < S.length(); j++) {
				char k = S.charAt(j);
				for (int h = 0; h < R; h++) {
					sb.append(k);
				}
			}
			sb.append("\n");
		}

		System.out.println(sb);

	}

}

 

 

- 1152번

package num5;

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

public class B1152 {

	public static void main(String[] args) throws IOException {

		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());
		System.out.println(st.countTokens());
	}

}

 

 

- 2908번

package num5;

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

public class B2908 {

	public static void main(String[] args) throws IOException {

		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());
		StringBuilder sb = new StringBuilder();

		for (int i = 0; i < 2; i++) {
			sb.append(st.nextToken() + " ");
		}
		sb.reverse();

		String str = sb.toString();
		st = new StringTokenizer(str);
		int comp1 = Integer.parseInt(st.nextToken());
		int comp2 = Integer.parseInt(st.nextToken());
		System.out.println(Math.max(comp1, comp2));
	}

}


//public class Main {
//	 
//	public static void main(String[] args) {
// 
//		Scanner in = new Scanner(System.in);
//        
//		int A = in.nextInt();
//		int B = in.nextInt();
//        
//        in.close();
//        
//		A = Integer.parseInt(new StringBuilder().append(A).reverse().toString());
//		B = Integer.parseInt(new StringBuilder().append(B).reverse().toString());
//		
//		System.out.print(A > B ? A : B);
//	
//	}
//}


//넣어주려는 인자가 String 타입이면 append()를 사용할 필요가 없다.
//위의 코드는 변수가 int 타입이라 StringBuilder 객체를 생성한 뒤 도트 연산자를 사용했지만,
//아래 코드는 객체를 생성하며 동시에 값을 할당한다.


//public class Main {
//	public static void main(String[] args) throws IOException {
// 
//		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//		
//		StringTokenizer st = new StringTokenizer(br.readLine()," ");
//        
//		int A = Integer.parseInt(new StringBuilder(st.nextToken()).reverse().toString());
//		int B = Integer.parseInt(new StringBuilder(st.nextToken()).reverse().toString());
//
//		System.out.print(A > B ? A:B);
//		
//	}
//}

→ 주석 코드 참고 블로그: https://st-lab.tistory.com/66

 

[백준] 2908번 : 상수 - JAVA [자바]

https://www.acmicpc.net/problem/2908 2908번: 상수 문제 상근이의 동생 상수는 수학을 정말 못한다. 상수는 숫자를 읽는데 문제가 있다. 이렇게 수학을 못하는 상수를 위해서 상근이는 수의 크기를 비교하

st-lab.tistory.com

 

 

- 5622번

package num5;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class B5622 {

	public static void main(String[] args) throws IOException {

		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String alphaNum = br.readLine();
		int count = 0;
		int k = alphaNum.length();

		for (int i = 0; i < k; i++) {
			switch (alphaNum.charAt(i)) {
			case 'A':
			case 'B':
			case 'C':
				count += 3;
				break;

			case 'D':
			case 'E':
			case 'F':
				count += 4;
				break;

			case 'G':
			case 'H':
			case 'I':
				count += 5;
				break;

			case 'J':
			case 'K':
			case 'L':
				count += 6;
				break;

			case 'M':
			case 'N':
			case 'O':
				count += 7;
				break;

			case 'P':
			case 'Q':
			case 'R':
			case 'S':
				count += 8;
				break;

			case 'T':
			case 'U':
			case 'V':
				count += 9;
				break;

			case 'W':
			case 'X':
			case 'Y':
			case 'Z':
				count += 10;
				break;
			}
		}
		System.out.println(count);
	}

}

 

 

- 11718번

package num5;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class B11718 {

	public static void main(String[] args) throws IOException {

		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String str;
		while ((str = br.readLine()) != null) {
			System.out.println(str);
		}
	}

}

 

728x90
반응형

'Algorithm > 백준(BOJ)' 카테고리의 다른 글

백준 6단계 - Java  (0) 2023.05.22
백준 4단계 - Java  (0) 2023.04.11
백준 3단계 - Java  (0) 2023.04.02
백준 2단계 - Java  (0) 2023.03.23
백준 1단계 - Java  (0) 2023.03.22

댓글