当前位置: 代码迷 >> 综合 >> [WikiOI] 2.4.2 m进制转十进制
  详细解决方案

[WikiOI] 2.4.2 m进制转十进制

热度:67   发布时间:2023-12-09 05:59:43.0

[Problem]

将m进制数n转化成一个十进制数 m<=16

题目保证转换后的十进制数<=100


[Solution]

#include <cstdio> #include <cstring> #include <iostream> using namespace std;#define MAX_LEN 101 char table[256];int main(){int m;char n[MAX_LEN];table['A'] = 10;table['B'] = 11;table['C'] = 12;table['D'] = 13;table['E'] = 14;table['F'] = 15;while(cin >> n >> m){int result = 0, base = 1;for(int i = strlen(n) - 1; i >= 0; --i){if(n[i] >= 'A' && n[i] <= 'F'){result += table[n[i]] * base;}else{result += (n[i] - '0') * base;}base *= m;}cout << result << endl;}return 0; }