PAT1058:A+B in Hogwarts
1058. A+B in Hogwarts (20)
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(),B();
string a,b;
while(cin >> a >> b)
{
int index = ;
//handle A
for(int i = ,j = ;i <a.size();i++)
{
string tmp;
if(a[i] == '.')
{
tmp = a.substr(j,i - j);
j = i + ;
A[index++] = stoi(tmp);
} if(index == )
{
tmp = a.substr(j,a.size() - j);
A[index++] = stoi(tmp);
}
}
//Handle B
index = ; for(int i = ,j = ;i <b.size();i++)
{
string tmp;
if(b[i] == '.')
{
tmp = b.substr(j,i - j);
j = i + ;
B[index++] = stoi(tmp);
}
if(index == )
{
tmp = b.substr(j,b.size() - j);
B[index++] = stoi(tmp);
}
} int add = ,sum = ;
sum = (A[] + B[]);
add = sum / ;
A[] = sum % ;
sum = A[] + B[] + add;
add = sum/;
A[] = sum % ;
A[] = A[] + B[] + add;
cout << A[] << "." <<A[] << "." << A[];
}
}
PAT1058:A+B in Hogwarts的更多相关文章
- PAT-1058 A+B in Hogwarts (进制转换)
1058. A+B in Hogwarts If you are a fan of Harry Potter, you would know the world of magic has its ow ...
- PAT1058. A+B in Hogwarts (20)
#include <iostream> using namespace std; int ag,as,ak; int bg,bs,bk; int cg,cs,ck; int main(){ ...
- PAT 1058 A+B in Hogwarts
1058 A+B in Hogwarts (20 分) If you are a fan of Harry Potter, you would know the world of magic ha ...
- 1058 A+B in Hogwarts (20 分)
1058 A+B in Hogwarts (20 分) If you are a fan of Harry Potter, you would know the world of magic has ...
- A1058 A+B in Hogwarts (20)(20 分)
A1058 A+B in Hogwarts (20)(20 分) If you are a fan of Harry Potter, you would know the world of magic ...
- pat 1058 A+B in Hogwarts(20 分)
1058 A+B in Hogwarts(20 分) If you are a fan of Harry Potter, you would know the world of magic has i ...
- PAT 甲级 1058 A+B in Hogwarts (20 分) (简单题)
1058 A+B in Hogwarts (20 分) If you are a fan of Harry Potter, you would know the world of magic ha ...
- PAT甲级——1058 A+B in Hogwarts
1058 A+B in Hogwarts If you are a fan of Harry Potter, you would know the world of magic has its own ...
- 1058 A+B in Hogwarts (20分)
1058 A+B in Hogwarts (20分) 题目: If you are a fan of Harry Potter, you would know the world of magic h ...
随机推荐
- 【一天一道LeetCode】#29. Divide Two Integers
一天一道LeetCode系列 (一)题目 Divide two integers without using multiplication, division and mod operator. If ...
- 滑动UITableViewCell出现多个按钮
iOS > = 5.0使用第三方效果图 iOS> = 8.0使用系统方法效果图 MGSwipeTableCell(Github上的三方库)- iOS >= 5.0 直接使用比较简单 ...
- mysql 好文章
http://my.oschina.net/OpenSourceBO/blog/353464 http://www.oschina.net/p/cobar mysql集群 http://www.dew ...
- 关于Android的https通讯安全
原文链接:http://pingguohe.net/2016/02/26/Android-App-secure-ssl.html 起因 前段时间,同事拿着一个代码安全扫描出来的 bug 过来咨询,我一 ...
- FFMPEG结构体分析:AVFormatContext
注:写了一系列的结构体的分析的文章,在这里列一个列表: FFMPEG结构体分析:AVFrameFFMPEG结构体分析:AVFormatContextFFMPEG结构体分析:AVCodecContext ...
- 用O_APPEND标志open一个文件,能否用lseek在任意位置读写
结论比较简单,用O_APPEND打开后,write操作是一个原子操作,所以每次都会自动把偏移量移到文件末尾,所以用lseek不能在任意位置write.但是可以用lseek在任意位置开始读.下面用代码测 ...
- Linux常用命令(第二版) --文件管理命令
文件管理命令 并不建议:照着像命令大全这类的书来学! 常用命令大约有200个. 文件命名规则: a)除了'/'之外所有字符都合法! b)这些字符最好不用 1.空格符,制表符,退格符 2.连接符 3.特 ...
- Leetcode_203_Remove Linked List Elements
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/45868027 Remove all elements fr ...
- 【Qt编程】基于Qt的词典开发系列<十一>系统托盘的显示
本文主要讨论Qt中的系统托盘的设置.系统托盘想必大家都不陌生,最常用的就是QQ.系统托盘以简单.小巧的形式能让人们较快的打开软件.废话不多说,下面开始具体介绍. 首先,新建一个Qt Gui项目,类型选 ...
- ActiveMQ系列之四:用ActiveMQ构建应用
Broker:相当于一个ActiveMQ服务器实例 命令行启动参数示例如下: 1:activemq start :使用默认的activemq.xml来启动 2:activemq start xbean ...