当前位置:网站首页>Basic exercise of test questions hexadecimal to decimal
Basic exercise of test questions hexadecimal to decimal
2022-07-01 04:39:00 【White phoenix leans on the sword and returns】
Resource constraints
The time limit :1.0s Memory limit :512.0MB
Problem description
Enter a number from the keyboard that does not exceed 8 A string of positive hexadecimal digits of , Convert it to a positive decimal number and output .
notes : In hexadecimal numbers 10~15 Use capital letters respectively A、B、C、D、E、F Express .
The sample input
FFFF
Sample output
65535
Topic analysis :
1. Test binary conversion
2.16 turn 10 Into the system for :
From the end, it is recorded as 0,s[i]*pow(16,0);
Add one from the end as 1,s[i]*pow(16,1);
And so on
The code is as follows :
C Language :
#include <stdio.h>
#include <math.h>
#include <string.h>
int main() {
char s[8];
scanf("%s", s);
long long int sum = 0;
int top = 0, last = strlen(s) - 1, i;
for (i = last; i >= 0; i--, top++) {
if (s[i] == 'A') {
sum += 10 * pow(16, top);
} else if (s[i] == 'B') {
sum += 11 * pow(16, top);
} else if (s[i] == 'C') {
sum += 12 * pow(16, top);
} else if (s[i] == 'D') {
sum += 13 * pow(16, top);
} else if (s[i] == 'E') {
sum += 14 * pow(16, top);
} else if (s[i] == 'F') {
sum += 15 * pow(16, top);
} else if (s[i] - '0' <= 9)
sum += (s[i] - '0') * pow(16, top);
}
printf("%ld", sum);
}C++:
#include <iostream>
#include <math.h>
using namespace std;
int main() {
string s;
cin >> s;
long long int sum = 0;
int top = 0, last = s.length() - 1;
for (int i = last; i >= 0; i--, top++) {
if (s[i] == 'A') {
sum += 10 * pow(16, top);
} else if (s[i] == 'B') {
sum += 11 * pow(16, top);
} else if (s[i] == 'C') {
sum += 12 * pow(16, top);
} else if (s[i] == 'D') {
sum += 13 * pow(16, top);
} else if (s[i] == 'E') {
sum += 14 * pow(16, top);
} else if (s[i] == 'F') {
sum += 15 * pow(16, top);
} else if (s[i] - '0' <= 9)
sum += (s[i] - '0') * pow(16, top);
}
cout << sum;
}边栏推荐
- CUDA development and debugging tool
- I also gave you the MySQL interview questions of Boda factory. If you need to come in and take your own
- 总结全了,低代码还需要解决这4点问题
- CF1638E. Colorful operations Kodori tree + differential tree array
- Introduction of Spock unit test framework and its practice in meituan optimization___ Chapter I
- OdeInt與GPU
- OSPF notes [multiple access, two multicast addresses with OSPF]
- Caijing 365 stock internal reference | the first IPO of Beijing stock exchange; the subsidiary of the recommended securities firm for gambling and gambling, with a 40% discount
- How do I sort a list of strings in dart- How can I sort a list of strings in Dart?
- 为什么香港服务器最适合海外建站使用
猜你喜欢

Grey correlation cases and codes
![Ospfb notes - five messages [ultra detailed] [Hello message, DD message, LSR message, LSU message, lsack message]](/img/aa/a255d225d71e6ba2b497f8d59f5f11.jpg)
Ospfb notes - five messages [ultra detailed] [Hello message, DD message, LSR message, LSU message, lsack message]

Custom components in applets

Programs and processes, process management, foreground and background processes

Introduction to JVM stack and heap

The index is invalid

CF1638E colorful operations

Obtain detailed ideas for ABCDEF questions of 2022 American Games
![[human version] Web3 privacy game in the dark forest](/img/89/e16789b7f3892002748aab309c45e6.png)
[human version] Web3 privacy game in the dark forest

Measurement of quadrature axis and direct axis inductance of three-phase permanent magnet synchronous motor
随机推荐
Introduction to JVM stack and heap
How to do the performance pressure test of "Health Code"
MySQL advanced -- you will have a new understanding of MySQL
Announcement on the list of Guangdong famous high-tech products to be selected in 2021
Odeint and GPU
ThreeJS开篇
VIM简易使用教程
Task04 mathematical statistics
All in all, the low code still needs to solve these four problems
I also gave you the MySQL interview questions of Boda factory. If you need to come in and take your own
[ue4] event distribution mechanism of reflective event distributor and active call event mechanism
OSPF notes [multiple access, two multicast addresses with OSPF]
After many job hopping, the monthly salary is equal to the annual salary of old colleagues
Tencent has five years of testing experience. It came to the interview to ask for 30K, and saw the so-called software testing ceiling
VR线上展览所具备应用及特色
Note de développement du système embarqué 80: application du concepteur Qt à la conception de l'interface principale
[deep learning] (4) decoder mechanism in transformer, complete pytoch code attached
Embedded System Development Notes 80: using QT designer to design the main interface
Custom components in applets
JS rotation chart