Problem Description
Many classmates said to me that A+B is must needs.
If you can’t AC this problem, you would invite me for night meal.
^_^
Input
Input may contain multiple test cases. Each case
contains A and B in one line.
A, B are hexadecimal number.
Input
terminates by EOF.
Output
Output A+B in decimal number in one line.
Sample Input
1 9
A B
a b
Sample Output
10
21
21
1 #include <stdio.h> 2 #include <string.h> 3 #include <ctype.h> 4 #include <math.h> 5 6 int decimal(char s[]); 7 8 int main(){ 9 char s1[50]; 10 char s2[50]; 11 int number1; 12 int number2; 13 14 while(scanf("%s%s",s1,s2)!=EOF){ 15 number1=decimal(s1); 16 number2=decimal(s2); 17 18 printf("%d\n",number1+number2); 19 20 } 21 22 return 0; 23 } 24 25 int decimal(char s[]){ 26 int result=0; 27 int i; 28 int length; 29 int temp; 30 31 length=strlen(s); 32 33 for(i=length-1;i>=0;i--){ 34 if(isdigit(s[i])) 35 temp=s[i]-‘0‘; 36 37 else if(s[i]==‘A‘ || s[i]==‘a‘) 38 temp=10; 39 40 else if(s[i]==‘B‘ || s[i]==‘b‘) 41 temp=11; 42 43 else if(s[i]==‘C‘ || s[i]==‘c‘) 44 temp=12; 45 46 else if(s[i]==‘D‘ || s[i]==‘d‘) 47 temp=13; 48 49 else if(s[i]==‘E‘ || s[i]==‘e‘) 50 temp=14; 51 52 else if(s[i]==‘F‘ || s[i]==‘f‘) 53 temp=15; 54 55 result+=temp*pow(16,length-1-i); 56 } 57 58 return result; 59 }
时间: 2024-12-27 15:52:19