如果你是哈利·波特迷,你會知道魔法世界有它自己的貨幣系統 —— 就如海格告訴哈利的:“十七個銀西可(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
Answer:
#include<iostream>using namespace std;struct money { int gal; int sic; int knu; inline money() { this->gal = 0; this->sic = 0; this->knu = 0; } inline void exp() { cout << gal << '.' << sic << '.' << knu; } inline void diff(money* a) { if(this->greater_equal(a)) { this->gal -= a->gal; this->sic -= a->sic; this->knu -= a->knu; if(this->knu < 0) { this->sic--; this->knu += 29; } if(this->sic < 0) { this->gal--; this->sic += 17; } } else { this->gal = a->gal - this->gal; this->sic = a->sic - this->sic; this->knu = a->knu - this->knu; if(this->knu < 0) { this->sic--; this->knu += 29; } if(this->sic < 0) { this->gal--; this->sic += 17; } this->gal *= -1; } } inline bool greater_equal(money *a) { if(this->gal > a->gal) return true; else if(this->gal < a->gal) return false; else if(this->sic > a->sic) return true; else if(this->sic < a->sic) return false; else if(this->knu > a->knu) return true; else if(this->knu < a->knu) return false; else return true; }};int main() { char pay[15]; char act[15]; money *p = new money(); money *a = new money(); cin >> pay >> act; int i = 0; while(pay[i] != '.') p->gal = p->gal*10 + pay[i++] - '0'; i++; while(pay[i] != '.') p->sic = p->sic*10 + pay[i++] - '0'; i++; while(pay[i]) p->knu = p->knu*10 + pay[i++] - '0'; i = 0; while(act[i] != '.') a->gal = a->gal*10 + act[i++] - '0'; i++; while(act[i] != '.') a->sic = a->sic*10 + act[i++] - '0'; i++; while(act[i]) a->knu = a->knu*10 + act[i++] - '0'; a->diff(p); a->exp();}PS. 又一次通過。 唉,膚淺如我啊。
新聞熱點
疑難解答