Search
moon
sun
📗

진법 변환

주차
문제번호
2745
언어
C++
티어
브론즈
유형
수학
구현
문자열
nj_Blog
nj_상태
이해도
100%
풀이
사람
이해도 2
13 more properties

문제접근🤔

입력받은 수를 뒤에서부터 순회하면서 진법에 맞게 곱해주어 더한다

놓쳤던 부분😅

string의 size()의 반환값도 unsigned int라고 생각해서 unsigned int로 선언하고 for문에서 i—를 통해서 i가 언더플로우가 나는 현상 발생
10이상의 수를 받을때와 아닐때를 구분짓지 않았음

코드😁

2212 KB

0 ms

#include <iostream> #include <string> #include <cmath> #include <cctype> using namespace std; int main(void) { string n; int b; int answer = 0; int cnt = 0; ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> b; for (int i = n.size() - 1; i >= 0; i--) { if (isalpha(n[i])) answer += ((n[i] - 'A' + 10) * pow(b, cnt++)); else answer += ((n[i] - '0') * pow(b, cnt++)); } cout << answer; return (0); }
C++
복사