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. ^_^
 

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
 不知道十六进制的时候,我直接相加。。。。
当在网上看到这么简单的方法,我惊呆了,真简单。。。
本来想多用几种方法做出来的,这里有两种方法。
方法一:直接利用%x
代码如下:
#include<stdio.h>
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;
}
posted @   wojiaohuangyu  阅读(6)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
点击右上角即可分享
微信分享提示