当前位置:网站首页>Convert string to decimal integer
Convert string to decimal integer
2022-07-03 14:15:00 【Study hard 867】
Enter a to # Ending string , This problem requires that all non hexadecimal characters be filtered out ( Case insensitive ), Form a new string of hexadecimal digits , Then convert it to decimal number and output . If there is a character before the first hexadecimal character “-”, It means that the number is negative .
Input format :
The input gives one in a line to # The ending non empty string .
Output format :
Output the converted decimal number on one line . Ensure that the output is in the long integer range .
sample input :
+-P-xf4+-1!#
sample output :
-3905Code :
#include <stdio.h>
int main() {
char a[50];
int b[50];
int i=0,sum=0,now=0,past=1,k,number=0,m=0;
while((a[i]=getchar())!='#') {
if(a[i]>='0'&&a[i]<='9') {
b[m++]=a[i]-'0';
number++;
} else if(a[i]>='A'&&a[i]<='F') {
b[m++]=a[i]-'7';
number++;
} else if(a[i]>='a'&&a[i]<='f') {
b[m++]=a[i]-'W';
number++;
}
i++;
}
now=i;
k=i;
sum=0;
for(i=0; i<number; i++) {
if(i>0) {
sum=sum*16+b[i];
} else if(i==0) {
sum=sum+b[i];
}
}
for(i=0; i<now; i++) {
if((a[i]>='0'&&a[i]<='9')||(a[i]>='A'&&a[i]<='F')||(a[i]>='a'&&a[i]<='f')) {
now=i;
break;
}
}
for(i=0; i<k; i++) {
if(a[i]=='-') {
past=i;
break;
}
}
if(past>now) {
printf("%d",sum);
} else {
printf("%d",-sum);
}
}边栏推荐
猜你喜欢

JS Part 2

Leetcode(4)——寻找两个正序数组的中位数

牛客网:过河卒

Scroll detection of the navigation bar enables the navigation bar to slide and fix with no content

Programmable logic device software testing

Why are grass-roots colleges and universities with "soil and poverty" called "Northeast small Tsinghua"?

剑指 Offer 28. 对称的二叉树

Similarities and differences between Allegro, OrCAD, net alias, port, off page connector and how to select them

7-18 finding the single root of polynomial by dichotomy

Polestar美股上市:5.5万台交付如何支持得起超200亿美元估值
随机推荐
Polestar美股上市:5.5万台交付如何支持得起超200亿美元估值
QT learning 20 standard dialog box in QT (middle)
天图投资冲刺港股:资产管理规模249亿 投了小红书与奈雪
Redis: redis data structure and key operation commands
7-15 calculation of PI
Metal organic framework (MOFs) antitumor drug carrier | pcn-223 loaded with metronidazole | uio-66 loaded with ciprofloxacin hydrochloride(
Simulated access
JVM garbage collector
编程语言:类型系统的本质
jvm-对象生命周期
28: Chapter 3: develop Passport Service: 11: define attributes in the configuration file, and then obtain them in the code;
Exercise 8-2 calculate the sum and difference of two numbers
泰凌冲刺科创板:拟募资13亿 国家大基金与小米长江是股东
GRPC的四种数据流以及案例
Understanding of closures
全局事件总线
Solution to failure or slow downloading of electron when electron uses electron builder to package
7-17 crawling worms (break exercise)
Nucleic acid modified metal organic framework drug carrier | pcn-223 metal organic framework encapsulated ad adamantane | zif-8 encapsulated adriamycin (DOX)
Facebook 如何将 Instagram 从 AWS 搬到自己的服务器