Blue Bridge Cup Test Questions Basic Practice Hexadecimal to Decimal (for 75 points)

Problem Description

  Input a positive hexadecimal number string with no more than 8 digits from the keyboard, convert it to a positive decimal number and output it.
  Note: 10~15 in hexadecimal numbers are represented by uppercase English letters A, B, C, D, E, and F respectively.

Sample input

FFFF

Sample output

65535

 Implementation code:

#include<stdio.h>
#include<math.h>
#include<string.h>
using namespace std;

int main()
{
	int n,i,j,a[100010];
	char c[100010];
	

	
		double sum=0;
		int r=1,t=0;
		scanf("%s",c);
		for(i=strlen(c)-1;i>=0;i--)
		{
			if(c[i] >= 'A'  && c[i] <= 'F')
		{
			sum += (c[i] - 'A'+10)*pow(16,t++); //默认pow的返回值是double型
	    }  
		else
		{
			sum += (c[i] - '0')*pow(16,t++);
		}
		
		}
		printf("%.0lf\n",sum);
    
    return 0;
}

75 minutes Cause analysis:

At the beginning, I used int to define sum, and I submitted 75 points. Then I changed it to long long. I tested the FFFFFFFF (8F) data, and it still overflowed .

So just use double , test it and submit it without overflow.

Guess you like

Origin blog.csdn.net/with_wine/article/details/114954392