A+B Coming |
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) |
Total Submission(s): 7516 Accepted Submission(s): 3406 |
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 主要用到sscanf这个库函数:
代码:
#include<iostream>
#include <stdio.h> using namespace std; void main() { char A[1000], B[1000]; int NA = 0, NB = 0; while (scanf("%s", A) != EOF) { sscanf(A, "%x", &NA); //printf("%d\n", NA); scanf("%s", B); sscanf(B, "%x", &NB); int sum = NA + NB; cout << sum << endl; } return; }
另外附上从别人blog里看到的手工转10进制代码:
//进制转换 //本题知识点: 字符串 //题目描述 // //写出一个程序,接受一个十六进制的数值字符串,输出该数值的十进制字符串。(多组同时输入 ) // //输入描述: //输入一个十六进制的数值字符串。 // //输出描述: //输出该数值的十进制字符串。 // //输入例子: //0xA // //输出例子: //10 #include<iostream> #include<string> #include<math.h> using namespace std; int main() { string s; int i=0,count,sum; while(getline(cin,s)) { int count=s.length(); sum=0; for(i=count-1;i>=0;i--)//从十六进制个位开始,每位都转换成十进制 { if(s[i]>='0'&&s[i]<='9')//数字字符的转换 { sum+=(s[i]-48)*pow(16,count-i-1); } else if(s[i]>='A'&&s[i]<='F')//字母字符的转换 { sum+=(s[i]-55)*pow(16,count-i-1); } } cout<<sum; } } |
HDU ACM 1.2.2 A+B Coming
最新推荐文章于 2019-03-05 15:17:01 发布