1058 A+B in Hogwarts
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>
using namespace std;
int main(){
int ga,sa,ka,gb,sb,kb,g=0,s=0,k=0;
scanf("%d.%d.%d %d.%d.%d",&ga,&sa,&ka,&gb,&sb,&kb);
int t=0;
k=kb+ka;
if(k>=29){
k%=29;
t=1;
}
s=t+sa+sb;
t=0;
if(s>=17){
s%=17;
t=1;
}
g=ga+gb+t;
printf("%d.%d.%d\n",g,s,k);
return 0;
}
看看大佬的,说不定有收获呢!
惭愧,又是膜拜的一天
#include <iostream>
using namespace std;
int main(){
int ga,sa,ka,gb,sb,kb,g=0,s=0,k=0;
scanf("%d.%d.%d %d.%d.%d",&ga,&sa,&ka,&gb,&sb,&kb);
k=(ka+kb)%29;s=(ka+kb)/29;
g+=(s+sa+sb)/17;s=(s+sa+sb)%17;
//因为s本身存了上一位的进位,需要先计算g,在计算s,如果先计算s,那么进位就没了
g+=ga+gb;
printf("%d.%d.%d\n",g,s,k);
return 0;
}
好好学习,天天向上!
我要考研!