A+B (杭電十六進制版本)

A+B Coming

Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8456 Accepted Submission(s): 3839
 

Problem Description

Many classmates said to me that A+B is must needs.
If you can’t AC this problem, you would invite me for night meal. ^_^

 

Input

Input may contain multiple test cases. Each case contains A and B in one line.
A, B are hexadecimal number.
Input terminates by EOF.

 

Output

Output A+B in decimal number in one line.

 

Sample Input

1 9
A B
a b

 

Sample Output

10
21
21

 

Author

威士忌

 

Source

HZIEE 2007 Programming Contest

 

Recommend

lcy

 

A + B即將來臨

時間限制:1000/1000 MS(Java / Others)內存限制:32768/32768 K(Java / Others)
提交總數:8456可接受的提交:3839
 

問題描述

許多同學對我說,A + B是必須的。
如果你不能解決這個問題,你會邀請我吃晚餐。^ _ ^

 

輸入

輸入可能包含多個測試用例。每個案例在一行中包含A和B. 
A,B是十六進制數。
輸入以EOF結束。

 

產量

在一行中以十進制數輸出A + B.

 

樣本輸入

1 9
AB
AB

 

樣本輸出

10
21
21

 

作者

威士忌

 

資源

HZIEE 2007編程大賽

 

推薦

LCY

 

我的思路是把輸入接受爲字符串string,從低位到高位每位成*(16^(n-1))這裏的16的n次方我使用的是快速冪,而字符轉數值我使用的是map函數

#include<stdio.h>
//#include<iostream>
#include<string.h>
#include<map>
using namespace std;
long long sww(int b){    //快速求冪
	int ans=1,a=16;
	while(b!=0){
		if(b&1!=0)ans*=a;
		a*=a;
		b>>=1;
	}
	return ans;
}
int main(){
	map<char,int>Mymap;
	Mymap['0']=0;
	Mymap['1']=1;
	Mymap['2']=2;
	Mymap['3']=3;
	Mymap['4']=4;
	Mymap['5']=5;
	Mymap['6']=6;
	Mymap['7']=7;
	Mymap['8']=8;
	Mymap['9']=9;
	Mymap['a']=10;
	Mymap['b']=11;
	Mymap['c']=12;
	Mymap['d']=13;
	Mymap['e']=14;
	Mymap['f']=15;
	Mymap['A']=10;
	Mymap['B']=11;
	Mymap['C']=12;
	Mymap['D']=13;
	Mymap['E']=14;
	Mymap['F']=15;
	char a[1000],b[1000];
	int A,B;
	while(scanf("%s%s",a,b)!=EOF){
		long long ans=0;
		A=strlen(a);
		B=strlen(b);
		for(int i=A-1,s=0;i>=0;i--,s++){
			ans+=Mymap[a[i]]*sww(s);
		}
		for(int i=B-1,s=0;i>=0;i--,s++){
			ans+=Mymap[b[i]]*sww(s);
		}
		printf("%lld\n",ans);
	}
	return 0;
}

當然我也知道 scanf("%x%x",&a,&b);可以直接輸入16進制數(●'◡'●)

(%x---16進制,%o---8進制 %c---字符 %s---字符串 %d ---int %ld---long  %lld --- long long %f--float %lf---double)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章