hdu-1720
A+B Coming
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5721 Accepted Submission(s): 3745
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. ^_^
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.
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
不知道十六进制的时候,我直接相加。。。。
当在网上看到这么简单的方法,我惊呆了,真简单。。。
本来想多用几种方法做出来的,这里有两种方法。
方法一:直接利用%x
代码如下:
#include<stdio.h>
int main(){
int a,b;
while(~scanf("%x %x",&a,&b)) //%x 表示的意思是输入十六进制
printf("%d\n",a+b); //大意了,这个压根就不知道
//没想到这么简单
return 0;
}
int main(){
int a,b;
while(~scanf("%x %x",&a,&b)) //%x 表示的意思是输入十六进制
printf("%d\n",a+b); //大意了,这个压根就不知道
//没想到这么简单
return 0;
}
方法二:
就是利用比较笨的方法,直接相加
#include<stdio.h>
#include<string.h>
int shi(char a){
if(a>='0'&&a<='9')
return a-'0';
else if(a>='A'&&a<='Z')
return a-'A'+10;
else if(a>='a'&&a<='z')
return a-'a'+10;
}
int main(){
char str1[1006];
char str2[100];
int i,j,k,t;
while(~scanf("%s %s",str1,str2)){
int a=0;
int b=0;
for(i=0;str1[i]!='\0';i++)
a=a*16+shi(str1[i]);
for(i=0;str2[i]!='\0';i++)
b=b*16+shi(str2[i]);
printf("%d\n",a+b);
}
return 0;
}
#include<string.h>
int shi(char a){
if(a>='0'&&a<='9')
return a-'0';
else if(a>='A'&&a<='Z')
return a-'A'+10;
else if(a>='a'&&a<='z')
return a-'a'+10;
}
int main(){
char str1[1006];
char str2[100];
int i,j,k,t;
while(~scanf("%s %s",str1,str2)){
int a=0;
int b=0;
for(i=0;str1[i]!='\0';i++)
a=a*16+shi(str1[i]);
for(i=0;str2[i]!='\0';i++)
b=b*16+shi(str2[i]);
printf("%d\n",a+b);
}
return 0;
}

浙公网安备 33010602011771号