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.
|
Input Input may contain multiple test cases. Each case contains A and B in one line.
|
Output Output A+B in decimal number in one line.
|
Sample Input
|
Sample Output
|
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.
|
樣本輸入
|
樣本輸出
|
作者 威士忌
|
資源 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)