컴퓨터기본/문제풀이

[백준] 11654번, 11720번, 10809번

차가운오미자 2021. 6. 14. 21:56

JAVA를 이용한 풀이

 

모두 문자열 관련 문제

 

11654번

import java.util.*;

public class Main{

	public static void main(String[] args) {
		
		int input;
		Scanner sc = new Scanner(System.in);
		input = sc.next().charAt(0);
		System.out.println(input);
	}

 

11720번

package baekjun1;

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		int num;
		String input;
		int result = 0;
		
		num = sc.nextInt();
		input = sc.next();
		
		for(int i = 0; i<num; i++) {
			result = result+(input.charAt(i)-48);
		}
		System.out.println(result);
	}
}

 

10809번

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		String input;
		
		int[] strarr = new int[26];
		for(int i = 0; i<26; i++) {
			strarr[i] = -1;
		}
		
		input = sc.next();
		
		for(int i = 0; i<input.length(); i++) {
			char temp = input.charAt(i);
			if(strarr[temp-'a'] == -1)
				strarr[temp-'a'] = i;
		}
		
		for(int i = 0; i<26; i++) {
			System.out.printf("%d ", strarr[i]);
		}
		
		System.out.println();
	}
}

주의할 점: 중복되는 알파벳이 있는 경우 재저장되지 않게, 배열에 값이 -1 인 애들만 바꿔줘야 한다.

'컴퓨터기본 > 문제풀이' 카테고리의 다른 글

[이것이~] 15. 구현  (0) 2021.06.14
[백준] 10828번, 1991번  (0) 2021.06.14
[이것이~] 탐욕법, 구현  (0) 2021.06.14
[프로그래머스] 정렬 > K번째수  (0) 2021.06.14
[백준] 2750번, 1181번, 1431번  (0) 2021.06.14