找零钱就是用大的数减去小的数。若实付小于所需,就在输出前添加一个负号。
#include <cstdio>
#include <algorithm>
#include <math.h>
#include <vector>
#include <iostream>
using namespace std;
struct money{
int Galleon;
int Sickle;
int Knut;
}P,A,END;
int jiafa(money P, money A, money &END){
if(A.Knut>=P.Knut) END.Knut=A.Knut-P.Knut;
else{
END.Knut=A.Knut-P.Knut+29;
A.Sickle--;
}
if(A.Sickle>=P.Sickle) END.Sickle=A.Sickle-P.Sickle;
else{
END.Sickle=A.Sickle-P.Sickle+17;
A.Galleon--;
}
END.Galleon=A.Galleon-P.Galleon;
if(END.Galleon>=0) return 1;
else return 0;
}
int main(){
int mark;
scanf("%d.%d.%d %d.%d.%d",&P.Galleon,&P.Sickle,&P.Knut,&A.Galleon,&A.Sickle,&A.Knut);
mark=jiafa(P,A,END);
if(mark) printf("%d.%d.%d",END.Galleon,END.Sickle,END.Knut);
else{
jiafa(A,P,END);
printf("-%d.%d.%d",END.Galleon,END.Sickle,END.Knut);
}
/*if(mark)*/
//else printf("-%d.%d.%d",END.Galleon,END.Sickle,END.Knut);
}
如需转载请私聊作者