A+B in Hogwarts (20)

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

#include<iostream>
#include<cstring>
using namespace std;
int main()
{
	char a[15],b[15];
	cin>>a>>b;
	int la = strlen(a);
	int lb = strlen(b);
	a[la] = '.';//作为后面转换计算的一种标志位 
	b[lb] = '.';
	int sa[3] = {0},sb[3] = {0};//数组初始化 
	int flag = 0;
	int s = 1;
	int k = 0;
	for(int i = 0;i <= la;i++)
	{
		if(a[i] == '.')
		{
			//将每一部分的字符串转换为int ,并存放在整型数组中 
			for(int j = i - 1;j >= flag;j--)
			{
				sa[k] = sa[k] + (int(a[j]) - 48) * s;
				s *= 10;
			}
			s = 1;
			flag = i + 1;
			k++;
		} 
	}
	flag = 0;
	k = 0;
	s = 1;
	for(int i = 0;i <= lb;i++)
	{
		if(b[i] == '.')
		{
			//将每一部分的字符串转换为int,并存放在整型数组中 
			for(int j = i - 1;j >= flag;j--)
			{
				sb[k] = sb[k] + (b[j] - 48) * s;
				s *= 10;
			}
			s = 1;
			flag = i + 1;
			k++;
		} 
	}
	int ca = 0,cb = 0;//a为第二部分相加的进位,b为第三部分相加的进位 
	int c1,c2,c3;//最终结果每一部分的数值 
	c3 = (sa[2] + sb[2]) % 29;
	cb = (sa[2] + sb[2]) / 29;
	c2 = (sa[1] + sb[1] + cb) % 17;
	ca = (sa[1] + sb[1] + cb) / 17;
	c1 = sa[0] + sb[0] + ca;
	cout<<c1<<'.'<<c2<<'.'<<c3;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Easadon/article/details/79940745
今日推荐