題目描述
如果你是哈利·波特迷,你會知道魔法世界有它自己的貨幣系統 —— 就如海格告訴哈利的:“十七個銀西可(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;
}