zoukankan      html  css  js  c++  java
  • PAT乙级题库“傻瓜”题解之在霍格沃茨找零钱

    如果你是哈利·波特迷,你会知道魔法世界有它自己的货币系统 —— 就如海格告诉哈利的:“十七个银西可(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
    #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;
     } 
     
  • 相关阅读:
    京东架构师:前端工程化在京东首页实践
    CSS 的命名和书写
    call,apply,bind方法的总结
    this指针指向的彻底理解
    Js获取当前日期时间及其它操作
    gulp-css-spriter 雪碧图合成
    Web前端业界氛围极好的群——鬼懿IT
    默认加载数据分页,点击分页后再次异步数据
    height设置百分比的条件
    input 的type类型值
  • 原文地址:https://www.cnblogs.com/solititude/p/11790117.html
Copyright © 2011-2022 走看看