如果你是哈利·波特迷,你会知道魔法世界有它自己的货币系统 —— 就如海格告诉哈利的:“十七个银西可(Sickle)兑一个加隆(Galleon),二十九个纳特(Knut)兑一个西可,很容易。”现在,给定哈利应付的价钱 P 和他实付的钱 A,你的任务是写一个程序来计算他应该被找的零钱。
输入格式:
输入在 1 行中分别给出 P 和 A,格式为 Galleon.Sickle.Knut
,其间用 1 个空格分隔。这里 Galleon
是 [0, 107] 区间内的整数,Sickle
是 [0, 17) 区间内的整数,Knut
是 [0, 29) 区间内的整数。
输出格式:
在一行中用与输入同样的格式输出哈利应该被找的零钱。如果他没带够钱,那么输出的应该是负数。
输入样例 1:
10.16.27 14.1.28
输出样例 1:
3.2.1
输入样例 2:
14.1.28 10.16.27
输出样例 2:
-3.2.1
#include<iostream>
#include<string>
#include<string.h>//一定要有这个不然VS2010过了但是题库没过。。。。。
using namespace std;
void fenli(int &a1,int &a2,int &a3,char *s1){
const char *c=new char[1];
c=".";
char *p=strtok(s1,c);
a1=atoi(p);
p=strtok(NULL,c);
a2=atoi(p);
p=strtok(NULL,c);
a3=atoi(p);
}
int c1,c2,c3;
void sub(int a1,int a2,int a3,int b1,int b2,int b3){
if(b3<a3){
c3=b3+29-a3;
--b2;
}else
c3=b3-a3;
if(b2<a2){
c2=b2+17-a2;
--b1;
}else
c2=b2-a2;
if(b1<a1){
c1=-1;
}else
c1=b1-a1;
}
int flag=0;
int main(){
while(1){
char s1[20],s2[20];
cin>>s1>>s2;
int a1,a2,a3,b1,b2,b3;
fenli(a1,a2,a3,s1);
fenli(b1,b2,b3,s2);
//cout<<a1<<" "<<a2<<" "<<a3<<endl;
//cout<<b1<<" "<<b2<<" "<<b3<<endl;
sub(a1,a2,a3,b1,b2,b3);
if(c1!=-1){
cout<<c1<<"."<<c2<<"."<<c3<<endl;
}else{
sub(b1,b2,b3,a1,a2,a3);
cout<<"-"<<c1<<"."<<c2<<"."<<c3<<endl;
}
}
}