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.27Sample Output:
14.1.28
参考代码:
#include <iostream>
#include <math.h>
using namespace std;
struct Node{
int a[3];
};
int str2int(string s){
int sum = 0;
for(int i=0;i<s.size();i++){
char ch = s[i];
sum += (ch-48)*pow(10,s.size()-i-1);
}
return sum;
}
int main()
{
Node arr[2];
string s;
for(int i=0;i<2;i++){
int index = 0;
cin>>s;
string temp = "";
for(int j=0;j<s.size();j++){
if(s[j]!='.'){
temp = temp+s[j];
}else{
arr[i].a[index++] = str2int(temp);
temp = "";
}
}
arr[i].a[index] = str2int(temp);
}
Node node;
int flag = 0;
if(arr[0].a[2]+arr[1].a[2]>28){
flag = 1;
node.a[2] = arr[0].a[2]+arr[1].a[2]-29;
}else{
node.a[2] = arr[0].a[2]+arr[1].a[2];
}
if(flag==1){
if(arr[0].a[1]+arr[1].a[1]>15){
node.a[1] = arr[0].a[1]+arr[1].a[1]+1-17;
}else{
node.a[1] = arr[0].a[1]+arr[1].a[1]+1;
flag = 0;
}
}else{
if(arr[0].a[1]+arr[1].a[1]>16){
node.a[1] = arr[0].a[1]+arr[1].a[1]-17;
flag = 1;
}else{
node.a[1] = arr[0].a[1]+arr[1].a[1];
flag = 0;
}
}
if(flag==1){
node.a[0] = arr[0].a[0]+arr[1].a[0]+1;
}else{
node.a[0] = arr[0].a[0]+arr[1].a[0];
}
cout<<node.a[0]<<"."<<node.a[1]<<"."<<node.a[2];
return 0;
}
本文介绍了一个基于哈利波特魔法世界货币系统的程序设计案例,通过解析输入格式为“Galleon.Sickle.Knut”的两种货币形式,并实现它们的加法运算,最终输出相同格式的计算结果。
612

被折叠的 条评论
为什么被折叠?



