Hduoj2057 【进制】【水题】

/*A + B Again
Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 15462    Accepted Submission(s): 6735


Problem Description
There must be many A + B problems in our HDOJ , now a new one is coming.
Give you two hexadecimal integers , your task is to calculate the sum of them,and print it in hexadecimal too.
Easy ? AC it !
 

Input
The input contains several test cases, please process to the end of the file.
Each case consists of two hexadecimal integers A and B in a line seperated by a blank.
The length of A and B is less than 15.
 

Output
For each test case,print the sum of A and B in hexadecimal in one line.

 

Sample Input
+A -A
+1A 12
1A -9
-1A -12
1A -AA
 

Sample Output
0
2C
11
-2C
-90
 

Author
linle
 

Source
校庆杯Warm Up 
*/
 
 
#include<stdio.h>
int main()
{
	__int64 a, b;
	while(scanf("%I64X%I64X", &a, &b) != EOF)
	{
		b+=a;
		if(b < 0)
		{
		
			b = -b;
			printf("-");
		}
		printf("%I64X\n", b);
	}
	return 0;
}
 
 
/*    纪念我死去的代码.....
#include<stdio.h>
#include<string.h> 
int main()
{
	char a[20], b[20];
	int i, j, k, fnum, flag, m, n, x[20], y[20], c[40];
	while(scanf("%s %s", a, b) != EOF)
	{
		getchar();
		fnum = 0;
		if( strchr(a, '-') )
		{
			fnum++;
			flag = 0;
		}
		if( strchr(b, '-') )
		{
			fnum++;
			flag = 1;
		}
		m = strlen(a);
		n = strlen(b);
		memset(x, 0, sizeof(x));
		memset(y, 0, sizeof(y));
		memset(c, 0, sizeof(c));
		j = 0;
		for(i = 0; i < m; i++)
		{
			if( a[m-1-i] != '+'  &&  a[m-1-i] != '-')
			{
				if(a[m-1-i] >= 65 && a[m-1-i] <= 90)
					x[j++] = a[m-1-i] - 55;
				else 
					x[j++] = a[m-1-i] - 48;
			}
		}
		m = j;
		j = 0;
		for(i = 0; i < n; i++)
		{
			if(b[n-1-i] != '+' && b[n-1-i] != '-')
			{
				if(b[n-1-i] >= 65 && b[n-1-i] <= 90)
				y[j++] = b[n-1-i] - 55;
				else 
				y[j++] = b[n-1-i] - 48;
			}
		}
		n = j;
		if(fnum == 0 || fnum == 2)
		{
			m = m>n?m:n;
			for(i = 0 ; i < m; i++)
			{
				c[i] += ( x[i] + y[i] );
				if(c[i] > 15)
				{
					c[i+1] += c[i] / 16;
					c[i] %= 16; 
				}
			}
			if( c[m] > 0)
			m++;
			if(fnum == 2)
			{
				if(m != 1 && c[0] != 0)
				printf("-");
			}
			for(i = m-1; i >= 0; i--)
			{
				if(c[i] < 10)
				printf("%d", c[i]);
				else 
				printf("%c", c[i]+55);
			}
			printf("\n");
		}
		else //flag
		{
			if(m > n)
			k = 0;
			else if( m < n)
			k = 1;
			else 
			{
				for(i = 0 ; i < m; i++)
				{
					if(x[i] > y[i])
					{
						k = 0; 
						break;
					}
					else  if( x[i] < y[i])
					{
						k = 1;
						break;
					}
				}
				if(i == m)
				k = -1;
			}
			if(k == 0)
			{
				for(i = 0; i < m; i++)
				{
					if(x[i] >= y[i])
					{
						x[i] -= y[i];
					}
					else
					{
						x[i] += 16;
						x[i+1] -= 1;
						x[i] -= y[i];
					}
				}
				if(flag == 0)
				printf("-");
				j = 0;
				for(i = m-1; i >= 0; i--)
				{
					if(x[i] != 0)
					{
						j = 1;
					}
					if(j)
					{
						if(x[i] < 10)
						printf("%d", x[i]);
						else
						printf("%c", x[i]+55);
					}
				}
				printf("\n");
			}
			else if(k == 1)
			{
				for(i = 0; i < n; i++)
				{
					if(y[i] >= x[i])
					{
						y[i] -= x[i];
					}
					else
					{
						y[i] += 16;
						y[i+1] -= 1;
						y[i] -= x[i];
					}
				}
				if(flag == 1)
				printf("-");
				j = 0;
				for(i = n-1; i >= 0; i--)
				{
					if(y[i] != 0)
					{
						j = 1;
					}
					if(j)
					{
						if(y[i] < 10)
						printf("%d", y[i]);
						else
						printf("%c", y[i]+55);
					}
				}
				printf("\n"); 
			}
			else
			printf("0\n");
		}
	} 
	return 0;
} 
*/


注意:输出时只能输出正的16进制数,所以负号需要自己手动输出。


你可能感兴趣的:(c)