728x90
반응형
문제
입력받은 16진수를 10진수로 변환합니다
코드
import java.util.Scanner;
public class Main {
public static void main(String [] args) {
Scanner s = new Scanner(System.in);
String input = s.nextLine();
int a, i, res=0;
for(i = 0; i < input.length(); i++) {
a = input.charAt(i);
if(a>=65)
a-=55;
else
a-=48;
res += Math.pow(16,(input.length())-i-1)*a;
}
System.out.println(res);
}
}
Math.pow()
Math.pow(a, b) : a의 b제곱을 계산
CharAt()
CharAt(i) : i 번째 문자를 char형으로 반환
728x90
728x90
'JAVA' 카테고리의 다른 글
[백준 2557번] Hello World! : java 출력 (0) | 2021.08.10 |
---|---|
[백준 2475번] 검증수 : for문 (0) | 2021.08.10 |
[백준2338번] 긴자리 계산 : BigInteger의 사칙연산 (0) | 2021.08.05 |
[백준1271번] 엄청난 부자2 : BigInteger 기초 (0) | 2021.08.04 |
[백준1001번] A-B : Scanner 객체 (0) | 2021.08.04 |
댓글