牛客网_华为机试_005_进制转换
2017-07-06 01:05
232 查看
题目描述
写出一个程序,接受一个十六进制的数值字符串,输出该数值的十进制字符串。(多组同时输入 )输入描述:
输入一个十六进制的数值字符串。
输出描述:
输出该数值的十进制字符串。
示例1
输入
0xA
输出
10
题目地址:https://www.nowcoder.com/practice/8f3df50d2b9043208c5eed283d1d4da6?tpId=37&tqId=21228&tPage=1&rp=&ru=/ta/huawei&qru=/ta/huawei/question-ranking
思路一:读入字符串,从第三位(0x之后)开始遍历字符串到结束,数字0~9 转换为数字,字母A~F转化为数字,根据位数判断该位的权值指数
#include <string> #include <iostream> using namespace std; int trans16to10(string str0x) { int result = 0; int n = str0x.size() - 3; for (int i = 2; i < str0x.size(); i++) { char c = str0x[i]; int num = 0; if ('0' <= c && c <= '9') { num = c - 48; } if ('A' <= c && c <= 'F') { num = 10 + (c - 'A'); } for (int j = 0; j < n; j++) { num *= 16; } n--; result += num; } return result; } int main() { string str0x; while(cin >> str0x) cout << trans16to10(str0x) << endl; return 0; }
思路二:常人无法理解
链接:https://www.nowcoder.com/questionTerminal/8f3df50d2b9043208c5eed283d1d4da6 来源:牛客网 #include <iostream> using namespace std; int main() { int a; while(cin>>hex>>a){ cout<<a<<endl; }
相关文章推荐
- 华为机试在线训练-牛客网(2)进制转换
- 华为机试在线训练-牛客网(37)计算字符相似度
- 【华为OJ】【005-进制转换】
- 牛客网_华为机试_013_句子逆序
- 华为机试在线训练-牛客网(19)【中级】单词倒排
- 华为机试在线训练-牛客网(28)查找组成一个偶数最接近的两个素数
- 华为机试在线训练-牛客网(31)配置文件恢复
- 牛客网--华为机试在线训练3:明明的随机数
- 牛客网_华为机试_006_质数因子
- 牛客网_华为机试_034_图片整理(字符排序)
- 华为机试在线训练-牛客网(16)字符串排序
- 华为机试在线训练-牛客网(29)DNA序列
- 牛客网_华为机试_007_取近似值
- 牛客网华为在线训练---进制转换
- 华为机试在线训练–牛客网(python)第二部分
- 华为机试在线训练-牛客网(3)字符串最后一个单词的长度
- 华为机试在线训练-牛客网(24)迷宫问题
- 华为机试在线训练-牛客网(32)成绩排序
- 牛客网-华为机试在线训练-1.求int型数据在内存中存储时1的个数
- 牛客网几道编程题(二)之华为机试在线训练:计算字符个数、字符串最后一个单词的长度、分隔字符串