题目描述
如果你是哈利·波特迷,你会知道魔法世界有它自己的货币系统 —— 就如海格告诉哈利的:“十七个银西可(Sickle)兑一个加隆(Galleon),二 十九个纳特(Knut)兑一个西可,很容易。”现在,给定哈利应付的价钱P和他实付的钱A,你的任务是写一个程序来计算他应该被找的零钱。
输入描述:
输入在1行中分别给出P和A,格式为“Galleon.Sickle.Knut”,其间用1个空格分隔。这里Galleon是[0, 107]]区间内的整数,Sickle是[0, 17)区间内的整数,Knut是[0, 29)区间内的整数。
输出描述:
在一行中用与输入同样的格式输出哈利应该被找的零钱。如果他没带够钱,那么输出的应该是负数。
输入例子:
10.16.27 14.1.28
输出例子:
3.2.1
代码:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string strp,stra;
cin>>strp>>stra;
int lack=0;
int nump[3]={0};
int numa[3]={0};
nump[0]=stoi(strp.substr(0,strp.find_first_of('.')));
nump[1]=stoi(strp.substr(strp.find_first_of('.')+1,strp.find_last_of('.')-strp.find_first_of('.')-1));
nump[2]=stoi(strp.substr(strp.find_last_of('.')+1,strp.size()-strp.find_last_of('.')));
numa[0]=stoi(stra.substr(0,stra.find_first_of('.')));
numa[1]=stoi(stra.substr(stra.find_first_of('.')+1,stra.find_last_of('.')-stra.find_first_of('.')-1));
numa[2]=stoi(stra.substr(stra.find_last_of('.')+1,stra.size()-stra.find_last_of('.')));
if((nump[0]*17*29+nump[1]*29+nump[2])>(numa[0]*17*29+numa[1]*29+numa[2]))
{
lack=1;
int temp;
for(int i=0;i<3;i++)
{
temp=nump[i];
nump[i]=numa[i];
numa[i]=temp;
}
}
string re="";
while(nump[2]>numa[2])
{
if(numa[1]==0)
{
numa[0]--;
numa[1]+=17;
}
numa[1]--;
numa[2]+=29;
}
re="."+to_string(numa[2]-nump[2])+re;
while(nump[1]>numa[1])
{
numa[0]--;
numa[1]+=17;
}
re="."+to_string(numa[1]-nump[1])+re;
re=to_string(numa[0]-nump[0])+re;
if(lack==1)
{
re="-"+re;
}
cout<<re;
return 0;
}