문제 링크입니다: www.acmicpc.net/problem/1550
1550번: 16진수
첫째 줄에 16진수 수가 주어진다. 이 수의 최대 길이는 6글자이다. 16진수 수는 0~9와 A~F로 이루어져 있고, A~F는 10~15를 뜻한다. 또, 이 수는 음이 아닌 정수이다.
www.acmicpc.net
간단한 수학 문제였습니다.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
using namespace std; | |
const int BASE = 16; | |
int power(int num, int idx) | |
{ | |
int result = num; | |
for (int i = 0; i < idx; i++) | |
{ | |
result *= BASE; | |
} | |
return result; | |
} | |
int convertHexToDec(char c) | |
{ | |
if ('A' <= c && 'F' >= c) { | |
return c - 'A' + 10; | |
} | |
return c - '0'; | |
} | |
int main(void) | |
{ | |
ios_base::sync_with_stdio(0); | |
cin.tie(0); | |
string hexNumber; | |
cin >> hexNumber; | |
int result = 0; | |
for (int i = 0; i < hexNumber.length(); i++) | |
{ | |
int num = convertHexToDec(hexNumber[i]); | |
result += power(num, hexNumber.length() - (i + 1)); | |
} | |
cout << result << "\n"; | |
return 0; | |
} |


개발환경:Visual Studio 2017
지적, 조언, 질문 환영입니다! 댓글 남겨주세요~
반응형
'알고리즘 > BOJ' 카테고리의 다른 글
[KOI 초등부] 백준 2475번 검증수 (0) | 2021.03.05 |
---|---|
백준 2338번 긴자리 계산 (C++) (0) | 2021.03.05 |
백준 사칙연산 문제들 모음 (0) | 2021.02.28 |
백준 15740번 A+B - 9 (C++) (0) | 2021.02.28 |
백준 2157번 여행 (0) | 2021.01.27 |