admin管理员组

文章数量:1794759

华为

华为

题目链接

www.nowcoder/practice/8f3df50d2b9043208c5eed283d1d4da6?tpId=37&tqId=21228&tPage=1&rp=&ru=/ta/huawei&qru=/ta/huawei/question-ranking

题目描述

写出一个程序,接受一个十六进制的数值字符串,输出该数值的十进制字符串。(多组同时输入 )

输入描述: 输入一个十六进制的数值字符串。 输出描述: 输出该数值的十进制字符串。

示例1

输入

复制

0xA 输出

复制

10

题解:

int l = str.length(); for(int i = l - 1; i >= 2; i--){ if(str[i] <= '9' && str[i] >= '0'){ int idx = str[i] - 48; ans += pow(16, l - i - 1) * idx; } else if(str[i] == 'A'){ int idx = 10; ans += pow(16, l - i - 1) * idx; } else if(str[i] == 'B'){ int idx = 11; ans += pow(16, l - i - 1) * idx; } else if(str[i] == 'C'){ int idx = 12; ans += pow(16, l - i - 1) * idx; } else if(str[i] == 'D'){ int idx = 13; ans += pow(16, l - i - 1) * idx; } else if(str[i] == 'E'){ int idx = 14; ans += pow(16, l - i - 1) * idx; } else if(str[i] == 'F'){ int idx = 15; ans += pow(16, l - i - 1) * idx; } } return ans; } int main(){ string str; while(cin >> str){ int ans = convert_to_decimal(str); cout << ans << endl; } return 0; }

 

本文标签: 华为