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, 10^7], 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
idea
- solution1中注意,p[i]可能超出int型范围,为防止数据失精,数组g,p都需要定的大一点,可为long long(直接定义为int型测试点2溢出
solution1
#include <stdio.h>
const int gallen = 29 * 17;
const int sickle = 29;
int main(){
int s[2], k[2];
long long g[2], p[2];
scanf("%lld.%d.%d%lld.%d.%d", g, s, k, g + 1, s + 1, k + 1);
for(int i = 0; i < 2; i++)
p[i] = g[i] * gallen + s[i] * sickle + k[i];
p[1] += p[0];
printf("%d.%d.%d", p[1] / gallen, p[1] % gallen / sickle, p[1] % sickle);
return 0;
}
solution2
#include <stdio.h>
int main(){
int g[3], s[3], k[3];
scanf("%d.%d.%d%d.%d.%d", g, s, k, g + 1, s + 1, k + 1);
k[2] = (k[0] + k[1]) % 29;
s[2] = (s[0] + s[1] + (k[0] + k[1] > 28 ? 1 : 0)) % 17;
g[2] = g[0] + g[1] + (s[0] + s[1] + (k[0] + k[1] > 28 ? 1 : 0) > 16 ? 1 : 0);
printf("%d.%d.%d", g[2], s[2], k[2]);
return 0;
}