Array-08. String converted to decimal integer

Source: Internet
Author: User
Tags pow

Array-08. String converted to decimal integer (15) time limit MS Memory limit 65536 KB code length limit 8000 B award Program StandardAuthor Zhang Tong Hongyu (Zhejiang University)

Enter a string ending with #, which requires filtering out all non-hexadecimal characters (not case), forming a new string representing a hexadecimal number, and then converting it to a decimal number after the output. If the first character of the filtered string is "-", it indicates that the number is negative.

Input format:

The input gives a non-empty string that ends with # in a row.

Output format:

Outputs the converted decimal number in a row. The title ensures that the output is within the long range.

Input Sample:
+p-xf4+-1!#
Sample output:
-3905
1#include <stdio.h>2#include <math.h>3#include <stdlib.h>4#include <string.h>5 intMain ()6 {7     intI, j =0, flag =1, sum =0;8     Charstr[ -], s[ -];9 gets (str);Ten      for(i =0; Str[i]! ='#'; i++) One     { A         if(Str[i] = ='-'&&flag) -S[j++] =Str[i]; -         if((Str[i] >='0'&& Str[i] <='9') || (Str[i]) >='a'&& Str[i] <='F'|| (Str[i] >='A'&& Str[i] <='F')) the         { -S[j++] =Str[i]; -Flag =0; -         } +     } -     if(s[0] !='-') +          for(i = j-1; I >=0; i--) A         { at             if(S[i] >='0'&& S[i] <='9') -Sum + = (S[i]-'0') * POW ( -, J-1-i); -             Else if(S[i] >='a'&& S[i] <='F') -Sum + = (S[i]-'a'+Ten) * POW ( -, J-1-i); -             Else if(S[i] >='A'&& S[i] <='F') -Sum + = (S[i]-'A'+Ten) * POW ( -, J-1-i); in         } -     Else to     { +          for(i = j-1; i >0; i--) -         { the             if(S[i] >='0'&& S[i] <='9') *Sum + = (S[i]-'0') * POW ( -, J-1-i); $             Else if(S[i] >='a'&& S[i] <='F')Panax NotoginsengSum + = (S[i]-'a'+Ten) * POW ( -, J-1-i); -             Else if(S[i] >='A'&& S[i] <='F') theSum + = (S[i]-'A'+Ten) * POW ( -, J-1-i); +         } Asum =-sum; the     } +printf"%d\n", sum); -     return 0; $}

Array-08. String converted to decimal integer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.