Description
Input an integer and its type. Output its two's complement in hexidecimal.
There are four types:
int includes 32 binary bits;
short includes 16 binary bits;
char includes 8 binary bits
longlong includes 64 binary bits
Input
Input an integer and it's type. such as
127 int
127 short
127 char
127 longlong
Output
The two's complement of them
0000007fH
007fH
7fH
000000000000007fH
主要是认识了short是%h,char是%hh。
#include <bits/stdc++.h> using namespace std; int main() { long long int num; int a; string st; while(scanf("%lld",&num)!=-1){ cin>>st; if (st=="short") { printf("%04hxH",(short)num); } if(st=="int") { printf("%08xH",(int)num); } if(st=="longlong") { printf("%016llxH",num ); } if(st=="char") { printf("%02hhxH", (char) num); } cout<<endl; } return 0; }