蓝桥杯BASIC-11 十六进制转十进制
题目:
思路:
将输入当做字符串逐个处理
代码:
1 #include<bits/stdc++.h> 2 using namespace std; 3 int main() 4 { 5 int len,times=0; 6 double num,sum=0; 7 char a[8]; 8 gets(a); 9 len = strlen(a); 10 for(int i=len-1;i>=0;i--) 11 { 12 num = 0; 13 if(a[i]>='0'&&a[i]<='9') 14 { 15 num = a[i]-'0'; 16 for(int j=0;j<times;j++) 17 { 18 num = num*16; 19 } 20 sum += num; 21 } 22 else if(a[i]>='A'&&a[i]<='F') 23 { 24 num = a[i]-'A'+10; 25 for(int j=0;j<times;j++) 26 { 27 num = num*16; 28 } 29 sum += num; 30 } 31 times++; 32 } 33 printf("%.0lf\n",sum); 34 return 0; 35 }