博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
华为研发工程师编程题----进制转换(pow函数,string.find())
阅读量:2387 次
发布时间:2019-05-10

本文共 1172 字,大约阅读时间需要 3 分钟。

[编程题] 进制转换

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

输入描述:
输入一个十六进制的数值字符串。
输出描述:
输出该数值的十进制字符串。
输入例子:
0xA
输出例子:
10
#include 
#include
#include
using namespace::std ;int main() { string input ; string index = "0123456789ABCDEF" ; while ( cin >> input ) { input = input.substr( 2 ) ; double base = input.size() - 1 ; double sum = 0.0 ; for ( int i = 0; i < input.size(); ++ i ) { int tmp = index.find( input[i] ) ; sum += tmp * pow( (double)16, base ) ; -- base ; } cout << sum << endl ; } return 0 ;}

第二次做:

#include 
#include
#include
using namespace::std ;int main() { static string index = "0123456789ABCDEF" ; string input ; while ( cin >> input ) { input = input.substr( 2 ) ; double base = input.size() - 1 ; double result = 0.0 ; for ( int i = 0; i < input.size(); ++ i ) { int tmp = index.find( input[i] ) ; result += tmp * pow( (double)16, base ) ; -- base ; } cout << result << endl ; } return 0 ;}

转载地址:http://jrnab.baihongyu.com/

你可能感兴趣的文章
python包含中文字符串长度
查看>>
sysbench 0.5 性能测试工具使用手册
查看>>
通过telnet连接查看memcache服务器
查看>>
django不用在数据库中创建新的user表而使用它的后台管理功能
查看>>
php array_unshift()修改数组key
查看>>
mysql性能优化-查询(Query)优化-2
查看>>
MySQL分区表的使用
查看>>
MongoDB 地理位置索引的实现原理
查看>>
MongoDB与MySQL的插入、查询性能测试
查看>>
深入理解OAuth2.0协议
查看>>
https原理:证书传递、验证和数据加密、解密过程解析
查看>>
MySQL在大型网站的应用架构演变
查看>>
sphinx教程1__mysql sphinx引擎插件式热安装
查看>>
sphinx教程2__安装、配置和使用
查看>>
ttserver 缓存使用和过期设置
查看>>
php pconnect 长连接原理
查看>>
php memcached使用中的坑
查看>>
php变量引用和计数_refcount_gc和is_ref_gc
查看>>
windows环境下php和Php扩展编译,扩展dll文件编译
查看>>
magento 验证码
查看>>