找零钱

链接:http://www.nowcoder.com/pat/6/problem/4063

题目描述

如果你是哈利·波特迷,你会知道魔法世界有它自己的货币系统 —— 就如海格告诉哈利的:“十七个银西可(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

思路:先把对应单位的值相减,分别得到每种单位的货币差多少(或者多多少),然后判断当前最大单位的货币的数量是正还是负。如果是负,那么就钱不够,就把所有为正的货币数修改为负。
14.1.28(应付的钱)
10.16.27 (拥有的钱)   差为:-4.15.-1 但是答案为:-3.2.1,-表示钱不够。将-4改为-3,那么去掉的那个‘-1’的加隆就要加到15西可上,‘-1’对应的又是-17个西可,所以就是
-3.-2.-1。一个负号-表示差,还差3.2.1个货币。
  1 #include "iostream"
  2 #include <iomanip>
  3 #include <string.h>
  4 #include <string>
  5 #include <vector>
  6 #include <cmath>
  7 #include <cctype>
  8 #include <algorithm>
  9 using namespace std;
 10  20 void change(string s, int *arr)
 21 {
 22     int i=0, k=0, value;
 23     memset(arr, 0, sizeof(arr));
 24     value = 0;
 25     while(s[i] != '.')
 26     {
 27         value = value*10+s[i++]-'0';
 28     }
 29     arr[k++] = value;
 30     ++i;
 31     value = 0;
 32     while(s[i] != '.')
 33     {
 34         value = value*10+s[i++]-'0';
 35     }
 36     arr[k++] = value;
 37     ++i;
 38     value = 0;
 39     while(i < s.length())
 40     {
 41         value = value*10+s[i++]-'0';
 42     }
 43     arr[k] = value;
 44 }
 45 int main()
 46 {
 47     string s1, s2;
 48     cin >>s1 >>s2;
 49     int price[3];
 50     int money[3];
 51     int res[3];
 52     change(s1, price);
 53     change(s2, money);
 54     int total = 0;
 55     total += (money[0]-price[0])*17*29;
 56     total += (money[1]-price[1])*29;
 57     total += (money[2]-price[2]);
 58     res[2] = total%29;
 59     total /= 29;
 60     res[1] = total%17;
 61     total /= 17;
 62     res[0] = total;
 63     if(res[0] > 0)
 64     {
 65         if(res[1] < 0)
 66         {
 67             --res[0];
 68             res[1] += 17;
 69         }
 70         if(res[2] < 0)
 71         {
 72             --res[1];
 73             res[2] += 29;
 74         }
 75     }
 76     if(res[0] < 0)
 77     {
 78         if(res[1] > 0)
 79         {
 80             ++res[0];
 81             res[1] -= 17;
 82         }
 83         if(res[2] > 0)
 84         {
 85             ++res[1];
 86             res[2] -= 29;
 87         }
 88     }
 89     if(res[0] == 0)
 90     {
 91         if(res[1] > 0)
 92         {
 93             if(res[2] < 0)
 94             {
 95                 --res[1];
 96                 res[2] += 29;
 97             }
 98         }
 99         if(res[1] < 0)
100         {
101             if(res[2] > 0)
102             {
103                 ++res[1];
104                 res[2] -= 29;
105             }
106         }
107     }
108     if(res[0]<0 || res[1]<0 || res[2]<0)
109     {
110         cout <<'-' <<-res[0] <<'.' <<-res[1] <<'.' <<-res[2] <<endl;
111     }
112     else
113     {
114         cout <<res[0] <<'.' <<res[1] <<'.' <<res[2] <<endl;
115     }
116     return 0;
117 }
原文地址:https://www.cnblogs.com/mtc-dyc/p/4630711.html