当前位置:网站首页>HDOJ-2057(A + B Again)
HDOJ-2057(A + B Again)
2022-07-16 01:29:00 【_rosy】
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
Inputcopy Outputcopy +A -A +1A 12 1A -9 -1A -12 1A -AA 0 2C 11 -2C -90
在此分享两种方法,一种是自己写的,另一种是大佬写的,看完别人写的才发现自己写的有多laji。。。
法一
#include<iostream>
#include<string>
#include<string.h>
#include<list>
#include<set>
#include<algorithm>
#include<math.h>
using namespace std;
typedef long long ll;
int a[100];
int len1,len2;
ll s1,s2;
ll solve(char c,int i,int t){
ll k,len;
t==1?len=len1:len=len2;
if(isdigit(c)){
k=c-'0';
k=k*pow(16,(len-i));
}
else{
if(c=='A'){
k=10;
k=k*pow(16,(len-i));
}
else if(c=='B'){
k=11;
k=k*pow(16,(len-i));
}
else if(c=='C'){
k=12;
k=k*pow(16,(len-i));
}
else if(c=='D'){
k=13;
k=k*pow(16,(len-i));
}
else if(c=='E'){
k=14;
k=k*pow(16,(len-i));
}
else if(c=='F'){
k=15;
k=k*pow(16,(len-i));
}
}
return k;
}
int main(){
string str1,str2;
while(cin>>str1>>str2){
len1=str1.size()-1;
len2=str2.size()-1;
s1=0,s2=0;
for(int i=str1.size()-1;i>=0;i--){
if(str1[i]=='+'){
break;
}
else if(str1[i]=='-'){
s1=-s1;
break;
}
s1=s1+solve(str1[i],i,1);
}
for(int i=str2.size()-1;i>=0;i--){
if(str2[i]=='+'){
break;
}
else if(str2[i]=='-'){
s2=-s2;
break;
}
s2=s2+solve(str2[i],i,2);
}
//cout<<s1+s2<<endl;
string s="";
ll sum=s1+s2;
bool flag=false;
if(sum==0){
cout<<0<<endl;
continue;
}
if(sum<0){
sum=-sum;
flag=true;
}
while(sum>0)
{
int y=sum%16;
if(y<10)
s=char('0'+y)+s;
else
s=char('A'-10+y)+s; //大于9的余数用ABCDE表示
sum=sum/16;
}
if(flag)
cout<<"-"<<s<<endl;
else cout<<s<<endl;
}
return 0;
}法二
别人写的:
#include<stdio.h>
int main()
{
long long n,m,v;
while(scanf("%llx%llx",&n,&m)==2)//llx是长整形的16进制
{
v=n+m;
if(v<0)
{
v=-v;
printf("-%llX\n",v);
}
else
printf("%llX\n",v);
}
return 0;
} 北冥有鱼,其名为鲲。鲲之大,不知其几千里也;化而为鸟,其名为鹏。鹏之背,不知其几千里也;怒而飞,其翼若垂天之云。是鸟也,海运则将徙于南冥。南冥者,天池也。《齐谐》者,志怪者也。《谐》之言曰:"鹏之徙于南冥也,水击三千里,抟扶摇而上者九万里,去以六月息者也。"野马也,尘埃也,生物之以息相吹也。天之苍苍,其正色邪?其远而无所至极邪?其视下也,亦若是则已矣。且夫水之积也不厚,则其负大舟也无力。覆杯水于坳堂之上,则芥为之舟;置杯焉则胶,水浅而舟大也。风之积也不厚,则其负大翼也无力。故九万里,则风斯在下矣,而后乃今培风;背负青天,而莫之夭阏者,而后乃今将图南。蜩与学鸠笑之曰:"我决起而飞,抢榆枋而止,时则不至,而控于地而已矣,奚以之九万里而南为?"适莽苍者,三餐而反,腹犹果然;适百里者,宿舂粮;适千里者,三月聚粮。之二虫又何知!小知不及大知,小年不及大年。奚以知其然也?朝菌不知晦朔,蟪蛄不知春秋,此小年也。楚之南有冥灵者,以五百岁为春,五百岁为秋;上古有大椿者,以八千岁为春,八千岁为秋。此大年也。而彭祖乃今以久特闻,众人匹之,不亦悲乎?
边栏推荐
- Heavyweight: Mobileye officially announced the postponement of IPO, slowing down the growth of revenue and intensifying market competition
- Ue5 simple role collision detection function
- Power BI----DAX讲解
- MySQL original field to hump naming
- 2022年成都/杭州/厦门/武汉产品经理认证招生简章(NPDP)
- Surpass traditional giants and take the top place in the list of senior engineers! What does this local enterprise rely on?
- 重建二叉树
- Deep learning ----- verification code
- 【对象转换】vo2dto使用
- Why is it said that big companies are not paradise? What pits are there?
猜你喜欢

Top100国内NFT平台 联盟链、公链使用情况统计

【微信小程序】简洁好用的icon(94/100)

JVM调优实战(详细版)
![[object header] view the bytes occupied by the object](/img/e5/226fa5858e1f4969a9d7cf8ff3c323.png)
[object header] view the bytes occupied by the object

逆向学习笔记(一)

Heavyweight: Mobileye officially announced the postponement of IPO, slowing down the growth of revenue and intensifying market competition

Use of gin framework

openEuler 知:repo

【对象转换】vo2dto使用

In three steps, I finished MySQL in one day, which made me win tmall offer smoothly
随机推荐
Image denoising using nlmeas
Surpass traditional giants and take the top place in the list of senior engineers! What does this local enterprise rely on?
Unity-NGUI的Prefab迁移到其他工程时脚本丢失解决办法
Jupyterab installation
Openeuler knowledge: log search skills
openEuler 知:SIG
openEuler 知:管理策略
Database system probability -- relational database
长安链介绍-01
Xunwei Godson development board domestic dual core 64 bit loognix system dual Gigabit Ethernet more interfaces
Differences among screenwidth, clientwidth, offsetwidth, and scrollwidth
Opensource knowledge: embedded software list
[object header] view the bytes occupied by the object
Completion report of communication software development and Application
Overview of database system -- overview of data model
Blazor University (36) component library
Solution to script loss when the prefab of unity ngui is migrated to other projects
Ue5 fonctions simples de détection des collisions de rôles
[jailhouse article] bao: a lightweight static partitioning hypervisor for modern multi core embedded
279. 完全平方数