[杭电]A + B Again
2014-05-04 15:18
288 查看
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2057
思路:由于十六进制数可以达到14位,顾需要用long long int 存储
代码:
#include <iostream>
#include <cmath>
#include <iomanip>
#include <string>
#include <map>
using namespace std;
long long to_decimal(string express)
{
long long res = 0;
bool negative = false;
if (express[0] == '-') negative = true;
long long begin = 0;
if (express[0] == '-' || express[0] == '+') begin++;
while (express[begin] != '\0')
{
long long bit = express[begin]-'0';
if (express[begin] >= 'A' && express[begin] <= 'F')
bit = express[begin] - 'A' + 10;
if (express[begin] >= 'a' && express[begin] <= 'f')
bit = express[begin] - 'a' + 10;
res = res * 16 + bit;
begin++;
}
return negative ? -res : res;
}
int main()
{
long long i, j, k;
string one, two;
while (cin >> one >> two)
{
long long res = to_decimal(one) + to_decimal(two);
if (res < 0)
{
cout << "-";
res = -res;
}
cout << hex << uppercase << res << endl;
}
return 0;
}
思路:由于十六进制数可以达到14位,顾需要用long long int 存储
代码:
#include <iostream>
#include <cmath>
#include <iomanip>
#include <string>
#include <map>
using namespace std;
long long to_decimal(string express)
{
long long res = 0;
bool negative = false;
if (express[0] == '-') negative = true;
long long begin = 0;
if (express[0] == '-' || express[0] == '+') begin++;
while (express[begin] != '\0')
{
long long bit = express[begin]-'0';
if (express[begin] >= 'A' && express[begin] <= 'F')
bit = express[begin] - 'A' + 10;
if (express[begin] >= 'a' && express[begin] <= 'f')
bit = express[begin] - 'a' + 10;
res = res * 16 + bit;
begin++;
}
return negative ? -res : res;
}
int main()
{
long long i, j, k;
string one, two;
while (cin >> one >> two)
{
long long res = to_decimal(one) + to_decimal(two);
if (res < 0)
{
cout << "-";
res = -res;
}
cout << hex << uppercase << res << endl;
}
return 0;
}
相关文章推荐
- 【杭电oj】1702 - ACboy needs your help again! (队列,栈之初体验)
- Doing Homework again(杭电1789)
- 【杭电1789】Doing Homework again贪心(还不太懂)
- HDOJ 继续水题,杭电2057 ,A+B again题目,关于进制输出的格式小技巧
- 【杭电oj】1702 - ACboy needs your help again!(栈和队列)
- 杭电acm1789 Dong Homework again
- 【最短路+dijkstra+spfa】杭电 hdu 2722 Here We Go(relians) Again
- 【杭电oj】1787 - GCD Again(欧拉函数)
- 【杭电】[1787]GCD Again
- 杭电 hdu 1867 A + B for you again
- 杭电 acm 2057 A+B Again
- 杭电1848 Fibonacci again and again(博弈-打表)
- 杭电 2523 SORT AGAIN(简单的标记,两个for循环即可)
- 【杭电-oj】-1702-ACboy needs your help again!(栈和队列基础)
- 杭电1789Doing Homework again
- 杭电1702 ACboy needs your help again!
- 杭电ACM1021裴波纳挈数AGAIN
- 【杭电 1789 】Doing Homework again
- 杭电ACM 2057 A + B Again
- 【杭电oj1702】ACboy needs your help again!