작성일 :

문제 링크

32929번 - UOS 문자열

설명

UOS를 무한히 반복한 문자열에서 x번째 문자를 구하는 문제입니다. 패턴 길이가 3이므로 (x-1) mod 3에 따라 U, O, S 중 하나가 됩니다.


접근법

idx = (x - 1) % 3 값을 계산해 0이면 U, 1이면 O, 2이면 S를 출력합니다.



Code

C#

1
2
3
4
5
6
7
8
9
10
using System;

class Program {
  static void Main() {
    long x = long.Parse(Console.ReadLine()!);
    long idx = (x - 1) % 3;
    char c = idx == 0 ? 'U' : idx == 1 ? 'O' : 'S';
    Console.WriteLine(c);
  }
}

C++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  ll x; cin >> x;
  ll idx = (x - 1) % 3;
  char c = (idx == 0) ? 'U' : (idx == 1) ? 'O' : 'S';
  cout << c << "\n";
  return 0;
}

Tags: 32929, BOJ, C#, C++, 구현, 백준, 알고리즘

Categories: ,