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;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理