본문으로 건너뛰기
풀이 목록으로 돌아가기

BOJ 4855 - Tire Dimensions

2026-03-21
BOJ
브론즈 II
cpp
원본 문제 보기
수학
문자열
파싱

문제

BOJ 4855 - Tire Dimensions

타이어 규격 WWW/RRRSLL 형식 문자열에서 폭(mm), 편평비(%), 림 지름(inch)을 추출해 외경(둘레)을 계산한 후 반올림한 값을 출력한다. 공식은 π * (2 * 폭 * 편평비/100 + 림 * 25.4).

풀이

문자열을 한 글자씩 읽으며 숫자를 모아 3개의 정수(width, aspect, rim)를 순서대로 파싱한다. 이후 공식대로 실수로 계산하고 round 후 정수로 출력한다. 원본 문자열도 함께 출력한다.

코드

#include <iostream>
#include <string>
#include <vector>
#include <cmath>
 
using namespace std;
 
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
 
  string line;
  while (getline(cin, line)) {
    if (line.empty()) continue;
 
    double v[3] = {0, 0, 0};
    int t = 0, current = -1;
 
    for (char c : line) {
      if (isdigit(c)) {
        if (current == -1) current = 0;
        current = current * 10 + (c - '0');
      } else if (current != -1) {
        if (t < 3) v[t++] = current;
        current = -1;
      }
    }
    if (current != -1 && t < 3) v[t++] = current;
 
    double result = ((v[0] / 10.0) * (v[1] / 100.0) * 2.0 + v[2] * 2.54) * M_PI;
    cout << line << ": " << (long long)round(result) << "\n";
  }
 
  return 0;
}

복잡도

  • 시간: O(L) (L: 입력 문자열 길이)
  • 공간: O(1)

최근 글