1058. A+B in Hogwarts (20)

1.给出两个数,根据题目要求进行加法运算。

2.该题不难,主要是字符串的处理和进制处理。

时间限制
50 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

If you are a fan of Harry Potter, you would know the world of magic has its own currency system — as Hagrid explained it to Harry, “Seventeen silver Sickles to a Galleon and twenty-nine Knuts to a Sickle, it’s easy enough.” Your job is to write a program to compute A+B where A and B are given in the standard form of “Galleon.Sickle.Knut” (Galleon is an integer in [0, 107], Sickle is an integer in [0, 17), and Knut is an integer in [0, 29)).

Input Specification:

Each input file contains one test case which occupies a line with A and B in the standard form, separated by one space.

Output Specification:

For each test case you should output the sum of A and B in one line, with the same format as the input.

Sample Input:

3.2.1 10.16.27

Sample Output:

14.1.28

AC代码:
[c language=”++”]
//#include<string>
//#include<stack>
//#include<unordered_set>
//#include <sstream>
//#include "func.h"
//#include <list>
#include <iomanip>
#include<unordered_map>
#include<set>
#include<queue>
#include<map>
#include<vector>
#include <algorithm>
#include<stdio.h>
#include<iostream>
#include<string>
#include<memory.h>
#include<limits.h>
#include<stack>
using namespace std;

bool isNum(char c)
{
if (c <= ‘9’&&c >= ‘0’) return true;
else return false;
}
void string2Num(string a,int&a1, int&a2, int&a3)
{
int idx = 0;
for (int i = 0; i < a.size(); i++)
{
if (idx == 0 && isNum(a[i]))
a1 = a1 * 10 + a[i] – ‘0’;
else if (idx == 1 && isNum(a[i]))
a2 = a2 * 10 + a[i] – ‘0’;
else if (idx == 2 && isNum(a[i]))
a3 = a3 * 10 + a[i] – ‘0’;
else if (a[i] == ‘.’)
{
idx++;
}
}
}
int main(void)
{

string a, b;
cin >> a >> b;
int a1 = 0, a2 = 0, a3 = 0;
int b1 = 0, b2 = 0, b3 = 0;

string2Num(a, a1, a2, a3);
string2Num(b, b1, b2, b3);

int carry = 0;
a3 = a3 + b3;
carry = a3 / 29;
a3 %= 29;
a2 = a2 + b2 + carry;
carry = a2 / 17;
a2 %= 17;
a1 = a1 + b1 + carry;

printf("%d.%d.%d\n", a1, a2, a3);

return 0;
}

[/c]

Leave a Reply

Your email address will not be published. Required fields are marked *