当前位置:网站首页>Acwing3452. 进制转换
Acwing3452. 进制转换
2022-08-08 11:13:00 【理工大猪猪】
写出一个程序,输入一个十六进制的数值字符串,输出该数值的十进制字符串。
输入格式
输入包含多组测试数据。
每组数据占一行,包含一个十六进制的数值字符串。
输出格式
每组数据输出一行结果,表示给定数值的十进制字符串。
数据范围
每个输入最多包含 100 组数据。
所有答案均在 int 范围内。
输入样例:
0xA
输出样例:
10思路
16进制数的第0位的权值为16的0次方,第1位的权值为16的1次方,第2位的权值为16的2次方……
所以,在第N(N从0开始)位上,如果是是数 X (X 大于等于0,并且X小于等于 15,即:F)表示的大小为 X * 16的N次方。
例:2AF5换算成10进制:
用竖式计算:
第0位: 5 * 16^0 = 5
第1位: F * 16^1 = 240
第2位: A * 16^2= 2560
第3位: 2 * 16^3 = 8192
10997
直接计算就是:
5 * 16^0 + F * 16^1 + A * 16^2 + 2 * 16^3 = 10997
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
string n;
int get(string n, int dex)
{
int res = 0, k = 0;
for (int i = n.size() - 1; i >= dex; i --)
{
if (n[i] >= 'A' && n[i] <= 'F') res += (n[i] - 'A' + 10) * pow(16, k ++);
else if (n[i] >= 'a' && n[i] <= 'f') res += (n[i] - 'a' + 10) * pow(16, k ++);
else res += (n[i] - '0') * pow(16, k ++);
}
return res;
}
int main()
{
while (cin >> n)
{
if (n[2] == 'x') cout << '-' << get(n, 3) << endl;
else cout << get(n, 2) << endl;
}
return 0;
}
边栏推荐
- CentOS MySQL体系管理
- #yyds干货盘点#【愚公系列】2022年08月 Go教学课程 005-变量
- Service Mesh迁移原则
- [Horizon Rising Sun X3 Trial Experience] WIFI connection, SSH login, TogetherROS installation (section 2)
- 微服务负载均衡器Ribbon实战
- Jingdong, zhang, director of the cloud wireless products division treasure jingdong cloud wireless treasure close relationship with the open source | the great god, open source BUFF gain strategy revi
- 一条SQL在MySQL中是如何执行的
- ReentrantLock源码分析和使用案例
- Kubernetes资源编排系列之四: CRD+Operator篇
- 2G 3G 4G 5G 基站覆盖范围
猜你喜欢

day01 -Web API介绍—DOM 介绍—获取元素—事件基础—操作元素—排他操作—自定义属性操作—节点操作—案例:动态生成表格—创建元素的三种方式(经典面试题)

详细讲解修改allure报告自定义的logo和名称中文

分布式系统设计策略

day01 - Introduction to Web API - Introduction to DOM - Getting Elements - Event Basics - Manipulating Elements - Exclusive Operations - Custom Attribute Operations - Node Operations - Cases: Dynamica

.NET Community Toolkit 8.0.0 版本发布

微服务分库分表
C语言详解系列——指针与结构体

关于振弦采集模块及采集仪振弦频率值准确率的问题

Mysql的分布式事务原理理解

Mysql索引优化实战
随机推荐
3D激光SLAM:LIO-SAM整体介绍与安装编译
300万招标!青岛市医疗保障局主机数据库中间件运行维护服务项目
vim /etc/profile 写入时 出现 E121:无法打开并写入文件解决方案
dedecms支持Word图文一键导入
图数据库一般用于什么时候呢?
关于win下面Celery服务报 Process 'Worker' exited with 'exitcode 1' [duplicate]
LeetCode_66_加一
使用ApacheBench来对美多商城的秒杀功能进行高并发压力测试
上海控安SmartRocket系列产品推介(二):SmartRocket Modeler可视化建模开发工具
LeetCode每日一题--有序队列(整理最小表示法)
写个 shell 玩 数字炸弹
带你深入理解3.4.2的版本更新,对用户带来了什么?
ReentrantReadWriteLock读写锁和票据锁StempedLock
day01 - Introduction to Web API - Introduction to DOM - Getting Elements - Event Basics - Manipulating Elements - Exclusive Operations - Custom Attribute Operations - Node Operations - Cases: Dynamica
Some optional strategies and usage scenarios for PWA application Service Worker caching
Kunpeng Developer Creation Day 2022: Kunpeng Full-Stack Innovation and Developers Build Digital Hunan
皕杰报表之数据校验与处理
轻量级接口自动化框架(jmeter+ant+jenkins)
Mysql的分布式事务原理理解
Hystrix熔断器