Hangdian ACM1720+2057 hexadecimal input and output

specific topic

http://acm.hdu.edu.cn/showproblem.php?pid=2057

http://acm.hdu.edu.cn/showproblem.php?pid=1720

Reprint address:

https://blog.csdn.net/xujinsmile/article/details/7836413

https://blog.csdn.net/i_fuqiang/article/details/8262844

Code: C language 1720+2057

#include<stdio.h>
intmain()
{
	int a,b;
	while(scanf("%x%x",&a,&b)!=EOF)
	{
		printf("%d\n",a+b);//For 2057, change to printf("%x\n",a+b);
	}
	return 0;
}

Code: C++1720+2057

#include<iostream>

#include<iomanip>

using namespace std;

int main(){
	__int64 a,b,c;
	while(cin>>hex>>a>>b){
		int c=0;
		c=a+b;
		if(c<0){
			cout<<"-";
			c=-c;
		}
		cout<<dec<<uppercase<<c<<endl;//2057:cout<<hex<<uppercase<<c<<endl;
	}
	return 0;
}

Note: what you need to understand is that the data stored by the computer is binary 0 1 so

scanf("%d",&a); is a decimal you enter. For example, you enter 10 (here is a decimal number) He also represents 10 (hexadecimal) The computer storage is 10000

scanf("%x",&a); is a hexadecimal you entered. For example, if you enter 10 (here is a hexadecimal number), it also represents 8 (decimal), and the computer storage is 1000

And when calculating b=a/15-273, the computer calculates with their respective binary representations, so in fact the obtained b exists in the computer as binary, only when you output the output by coercion, it becomes us Desired decimal or hexadecimal such as: When b is output, use hexadecimal output, that is, printf("%x",b);


C++ also provides hexadecimal addition

Header file: #include <iomanip>
Description: It is an I/O flow control header file, just like the formatted output in C

         controller                             Use                                     
 dec   set integer to decimal
 hex  set integer to hexadecimal
 oct  set integer to octal
 setbase(n)  Set integer to n base (n=8,10,16)
 setfill(n)

 Set character padding, c can be a character constant or a character variable

 setprecision(n)  Set the significant digits of a floating-point number to n digits
 setw(n)  Set the field width to n bits
 setiosflags(ios::fixed)  Set floating point numbers to be displayed with a fixed number of decimal places
 setiosflags(ios::scientific)    Set floating point numbers to be represented in scientific notation
 setiosflags(ios::left)  output left justified
 setiosflags(ios::right)  output right justified
 setiosflags(ios::skipws)  Ignore leading spaces
 setiosflags(ios::uppercase)  In scientific notation output E and hexadecimal output X in uppercase, otherwise lowercase.
 setiosflags(ios::showpos)  Display "+" sign when outputting positive numbers
 setiosflags(ios::showpoint)  Force a decimal point
 resetiosflags () 

 Terminates the output format state that has been set, which should be specified in parentheses


The letters in the output of hex are lowercase by default, and uppercase needs to be added

Negative hexadecimal numbers are represented in the computer's complement form. To display the hexadecimal form with positive and negative signs, it is necessary to invert it and add a negative sign, -c!

fool

2057
#include<iostream>
#include<iomanip>
using namespace std;

int main(){
	__int64 a,b,c;
	while(cin>>hex>>a>>b){
		int c=0;
		c=a+b;
		if(c<0){
			cout<<"-";
			c=-c;
		}
		cout<<hex<<uppercase<<c<<endl;
	}
	return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325979682&siteId=291194637