SOLUTION INFO
C++ · main.cpp
- 작성자
- tony9402
- 공동 작성자
- 없음
#include<bits/stdc++.h>
using namespace std;
int getValue(char x) {
if(x <= '9') return x - '0';
return x - 'A' + 10;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
string s; cin >> s;
int base; cin >> base;
int answer = 0;
for(int i=0;i<(int)s.size();i++) {
answer = answer * base + getValue(s[i]);
}
cout << answer;
return 0;
}
SOLUTION DESCRIPTION
풀이 설명
등록된 풀이 설명이 없습니다.
SOLUTION INFO
Java · Main.java
- 작성자
- suin8
- 공동 작성자
- 없음
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) {
FastReader rd = new FastReader();
String N = rd.next();
int B = rd.nextInt();
int num = 0;
int digit = 0;
// 문자 A~Z int형 변환 시(65~90) 따라서 -55를 하면
// A는 10 B는 11 ... 이런식으로 나오게 된다.
// 문자 0~9 int형 변환 시(48~57) 따라서 -48을 하면
// 0 ~ 9가 나온다.
for(int i = N.length() - 1; i >= 0; i--) {
if(N.charAt(i) >= 65)
num += (N.charAt(i) - 55) * Math.pow(B, digit);
else
num += (N.charAt(i) - 48) * Math.pow(B, digit);
digit++;
}
System.out.print(num);
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while(st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
String nextLine() {
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
SOLUTION DESCRIPTION
풀이 설명
등록된 풀이 설명이 없습니다.