(구) solved ac class 문제들/class 1

[class 1]백준 문자와 문자열(27866번) 풀이 (C++/Python)

isekaipudding 2026. 4. 14. 12:59

문제 : 문자와 문자열(27866번)(Bronze V)

문제 링크 : https://www.acmicpc.net/problem/27866
출처 : Baekjoon Online Judge

 

문자열 기초에서 문자열 인덱싱이 가능한 것을 알게 되었습니다.

보통 index는 0부터 size - 1까지이며 이것을 0-based index라고 부릅니다.

하지만 이번에 해결할 문제는 1-based index이며 1부터 size까지입니다.

예시로 size = 5라고 가정하면 0-based index는 0부터 4까지, 1-based index는 1부터 5까지입니다.

하지만 1-based index는 사람이 읽기 편하게 되어 있지만 컴퓨터는 1-based index를 이해하지 못 합니다.

그러므로 1-based index를 0-based index로 변환하기 위해 -1을 해야 합니다.

 

아래는 1-based index로 입력하여 0-based index로 변환 후 인덱싱하는 소스 코드입니다.

[C++]

#include <bits/stdc++.h>
 
using namespace std;
 
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    string input;
    cin >> input;

    int index;
    cin >> index;

    cout << input[index-1];

    return 0;
}

[Python]

import sys

input = sys.stdin.readline

s:str = input().rstrip()
i:int = int(input().rstrip())

print(s[i-1])