PAT1058:A+B in Hogwarts

Posted 0kk470

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PAT1058:A+B in Hogwarts相关的知识,希望对你有一定的参考价值。

1058. A+B in Hogwarts (20)

时间限制
50 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

If you are a fan of Harry Potter, you would know the world of magic has its own currency system -- as Hagrid explained it to Harry, "Seventeen silver Sickles to a Galleon and twenty-nine Knuts to a Sickle, it‘s easy enough." Your job is to write a program to compute A+B where A and B are given in the standard form of "Galleon.Sickle.Knut" (Galleon is an integer in [0, 107], Sickle is an integer in [0, 17), and Knut is an integer in [0, 29)).

Input Specification:

Each input file contains one test case which occupies a line with A and B in the standard form, separated by one space.

Output Specification:

For each test case you should output the sum of A and B in one line, with the same format as the input.

Sample Input:
3.2.1 10.16.27
Sample Output:
14.1.28

思路
加法,注意进位就好。

注:
1.这道题看着很简单,然而用cin输入的是两个字符串,还得将字符串分割处理,然后转换成int,有点坑。
2.直接用scanf("%d.%d.%d",&x,&y,&z)读数据就不用处理字符串 =_=!。

代码
#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main()
{
    vector<int> A(3),B(3);
    string a,b;
    while(cin >> a >> b)
    {
        int index = 0;
        //handle A
        for(int i = 0,j = 0;i <a.size();i++)
        {
           string tmp;
           if(a[i] == .)
           {
              tmp = a.substr(j,i - j);
              j = i + 1;
              A[index++] = stoi(tmp);
           }

           if(index == 2)
           {
             tmp = a.substr(j,a.size() - j);
             A[index++] = stoi(tmp);
           }
        }
        //Handle B
        index = 0;

        for(int i = 0,j = 0;i <b.size();i++)
        {
           string tmp;
           if(b[i] == .)
           {
              tmp = b.substr(j,i - j);
              j = i + 1;
              B[index++] = stoi(tmp);
           }
           if(index == 2)
           {
             tmp = b.substr(j,b.size() - j);
             B[index++] = stoi(tmp);
           }
        }

        int add = 0,sum = 0;
        sum = (A[2] + B[2]);
        add = sum / 29;
        A[2] = sum % 29;
        sum = A[1] + B[1] + add;
        add = sum/17;
        A[1] = sum % 17;
        A[0] = A[0] + B[0] + add;
        cout << A[0] << "." <<A[1] << "." << A[2];
    }
}

 

以上是关于PAT1058:A+B in Hogwarts的主要内容,如果未能解决你的问题,请参考以下文章

pat 1058 A+B in Hogwarts(20 分)

PAT甲级——A1058 A+B in Hogwarts

PAT 甲级 1058 A+B in Hogwarts (20 分) (简单题)

PAT A1058 A+B in Hogwarts (20)[进制转换]

PAT (Advanced Level) 1058. A+B in Hogwarts (20)

PAT-进制转换-A1058 A+B in Hogwarts (20分)