HDOJ-2057(A + B Again)

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

InputcopyOutputcopy
 
+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;
 } 

北冥有鱼,其名为鲲。鲲之大,不知其几千里也;化而为鸟,其名为鹏。鹏之背,不知其几千里也;怒而飞,其翼若垂天之云。是鸟也,海运则将徙于南冥。南冥者,天池也。《齐谐》者,志怪者也。《谐》之言曰:"鹏之徙于南冥也,水击三千里,抟扶摇而上者九万里,去以六月息者也。"野马也,尘埃也,生物之以息相吹也。天之苍苍,其正色邪?其远而无所至极邪?其视下也,亦若是则已矣。且夫水之积也不厚,则其负大舟也无力。覆杯水于坳堂之上,则芥为之舟;置杯焉则胶,水浅而舟大也。风之积也不厚,则其负大翼也无力。故九万里,则风斯在下矣,而后乃今培风;背负青天,而莫之夭阏者,而后乃今将图南。蜩与学鸠笑之曰:"我决起而飞,抢榆枋而止,时则不至,而控于地而已矣,奚以之九万里而南为?"适莽苍者,三餐而反,腹犹果然;适百里者,宿舂粮;适千里者,三月聚粮。之二虫又何知!小知不及大知,小年不及大年。奚以知其然也?朝菌不知晦朔,蟪蛄不知春秋,此小年也。楚之南有冥灵者,以五百岁为春,五百岁为秋;上古有大椿者,以八千岁为春,八千岁为秋。此大年也。而彭祖乃今以久特闻,众人匹之,不亦悲乎?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

H-rosy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值