A+B in Hogwarts

来源:互联网 发布:大数据 统计区别 编辑:程序博客网 时间:2024/05/17 03:14

题目描述

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)).

输入描述:

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


输出描述:

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

输入例子:

3.2.1 10.16.27

输出例子:

14.1.28

代码:

#include"iostream"
#include "string"
#include "cmath"
#include "vector"
#include<iomanip>
using namespace std;


struct Standard
{
int Galleon = 0;
int Sickle = 0;
int Knut = 0;
};


int main()
{
string firstNum, secondNum;
cin >> firstNum >> secondNum;
int i = 0;
Standard first, second;
int key = 0;
for(int i = 0;i < firstNum.size(); i++)
{
if (firstNum[i] == '.')
{
key++;
continue;
}
switch (key)
{
case 0:
first.Galleon = 10 * first.Galleon + (firstNum[i] - '0');
break;
case 1:
first.Sickle = 10 * first.Sickle + (firstNum[i] - '0');
break;
case 2:
first.Knut = 10 * first.Knut + (firstNum[i] - '0');
default:
break;
}
}
key = 0;
for (int i = 0; i < secondNum.size(); i++)
{
if (secondNum[i] == '.')
{
key++;
continue;
}
switch (key)
{
case 0:
second.Galleon = 10 * second.Galleon + (secondNum[i] - '0');
break;
case 1:
second.Sickle = 10 * second.Sickle + (secondNum[i] - '0');
break;
case 2:
second.Knut = 10 * second.Knut + (secondNum[i] - '0');
default:
break;
}
}
Standard result;
result.Knut = first.Knut + second.Knut;
result.Sickle = first.Sickle + second.Sickle;
result.Galleon = first.Galleon + second.Galleon;
if (result.Knut >= 29)
{
result.Knut -= 29;
result.Sickle++;
}
if (result.Sickle >= 17)
{
result.Sickle -= 17;
result.Galleon++;
}
printf("%d.%d.%d", result.Galleon, result.Sickle, result.Knut);
}

0 0
原创粉丝点击