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