首页 > 其他 > 详细

PAT乙级题库“傻瓜”题解之在霍格沃茨找零钱

时间:2019-11-04 09:51:38      阅读:80      评论:0      收藏:0      [点我收藏+]

如果你是哈利·波特迷,你会知道魔法世界有它自己的货币系统 —— 就如海格告诉哈利的:“十七个银西可(Sickle)兑一个加隆(Galleon),二十九个纳特(Knut)兑一个西可,很容易。”现在,给定哈利应付的价钱 P 和他实付的钱 A,你的任务是写一个程序来计算他应该被找的零钱。

输入格式:

输入在 1 行中分别给出 P 和 A,格式为 Galleon.Sickle.Knut,其间用 1 个空格分隔。这里 Galleon 是 [0, 10?7??] 区间内的整数,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
#include<iostream>
#include<string>
#include<stdlib.h>
using namespace std;
int main()
{
    string money;
    cin>>money;
    getchar();//string不能读取字符串中的空格,这里用getchar吸收掉一个空格
     
    string money2;
    cin>>money2;
    string str1,str2,str3,str_1,str_2,str_3;
    
    for(int i=0;i<money.length();i++)
    {
        if(money[i]==.)
        {
            while(money[i+1]!=.)
            {
                i++;
                str2+=money[i];            }
            i++;
            while(money[i+1]!=.&&money[i+1]<=9&&money[i+1]>=0)
            {
                i++;
                str3+=money[i];
            }    
        }
        else
        {
            str1+=money[i];
        }
    }
    for(int i=0;i<money2.length();i++)
    {
        if(money2[i]==.)
        {
            
            while(money2[i+1]!=.)
            {
                i++;
                str_2+=money2[i];
            }
        
            i++;
            while(money2[i+1]!=.&&money2[i+1]<=9&&money2[i+1]>=0)
            {
                i++;
                str_3+=money2[i];
                        
            }
        }
        else
        {
            str_1+=money2[i];
        }
        
    }
        
    //将货币转化为纳特来计算 
    char *end,*end1,*end2,*end3,*end4,*end5;
    long a=strtol(str1.c_str(),&end,10)*17*29,
    b=strtol(str2.c_str(),&end1,10)*29,
    c=strtol(str3.c_str(),&end2,10),
    a1=strtol(str_1.c_str(),&end3,10)*17*29,
    b1=strtol(str_2.c_str(),&end4,10)*29,
    c1=strtol(str_3.c_str(),&end5,10);
    //将字符串转化为长整型数据
    
     

    long sum=a1+b1+c1-a-b-c;
    long A,B,C;
    A=sum/(29*17);
    B=(sum-A*17*29)/29;
    C=sum-A*29*17-B*29;
    if(sum>=0)
    {
        cout<<A<<.<<B<<.<<C<<endl;
    }
    else
    {
        cout<<A<<.<<0-B<<.<<0-C<<endl; 
    }
     

    return 0;
 } 

 

 

PAT乙级题库“傻瓜”题解之在霍格沃茨找零钱

原文:https://www.cnblogs.com/solititude/p/11790117.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!