백준C(일반수학)

쿵ㅇ양·2024년 1월 10일

알고리즘

목록 보기
2/18

백준2745-진법 변환

B진법 수 N이 주어진다. 이 수를 10진법으로 바꿔 출력하는 프로그램을 작성하시오.
10진법을 넘어가는 진법은 숫자로 표시할 수 없는 자리가 있다. 이런 경우에는 다음과 같이 알파벳 대문자를 사용한다.
A: 10, B: 11, ..., F: 15, ..., Y: 34, Z: 35

진법 변환 원리???

ex1) 10진법

ex2)2진법

n진법 -> 10진법

  • 문자열 -> 정수 변환 주의!!(아스키 코드 이용)
  • 제곱 = 누적해서 곱해주기
#include<stdio.h>
#include<string.h>

int main(){
    int n;
    char str[1000];
    int len, k, num;
    int i = 0;
    int result = 0;
    
    scanf("%s %d", str, &n);
    
    len = strlen(str);
    
    while (str[i]){
        k = i;
        if(str[i]>= 'A' && str[i]<='Z'){
            num = str[i]-'A'+10;
        }
        if(str[i]>='0' && str[i]<= '9')
            num = str[i]-'0';
        while(len-k-1>0){
            num = num * n;
            k++;
        }
        result = result + num;
        i++;
    }
    printf("%d", result);
    
    return 0;
}```
profile
개발을 공부하고 있는 대학생

0개의 댓글