码迷,mamicode.com
首页 > 其他好文 > 详细

运算符重载在ACM中的运用

时间:2015-03-31 09:22:03      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:c++   acm   

运算符重载问题是C++中经典的问题,使用运算符重载可以实现不同的类型数据的直接操作,比如复数问题等

针对此部分进行联系

练习题目:

A+B in Hogwarts

问题描述:

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


源码:

#include <iostream>
#include <cstdio>

using namespace std;

class strange_num
{
private:
	long long Galleon;
	int Sickle;
	int Knut;

public:
	strange_num():Galleon(0),Sickle(0),Knut(0){}
	strange_num(long long,int, int);
	~strange_num(){};
	friend strange_num operator+( strange_num& aa, strange_num& pp);
	long long get_Galleon()
	{
		return Galleon;
	}
	int get_Sickle()
	{
		return Sickle;
	}
	int get_Knut()
	{
		return Knut;
	}
	
};

strange_num::strange_num(long long Galleons,int Sickles, int Knuts)
{
	int rr=0;
	rr=Knuts/29;
	this->Knut=Knuts%29;

	this->Sickle=(Sickles+rr)%17;
	rr=(Sickles+rr)/17;
	
	this->Galleon=Galleons+rr;
	
}
strange_num operator+( strange_num& aa, strange_num& pp)
{
	return strange_num (aa.Galleon+pp.Galleon,aa.Sickle+pp.Sickle,aa.Knut+pp.Knut);
}

int main(void)
{
	long long a;
	int b,c;
	scanf("%lld.%d.%d",&a,&b,&c);
	strange_num tt(a,b,c);
	scanf("%lld.%d.%d",&a,&b,&c);
	strange_num cc(a,b,c);
	strange_num mm=tt+cc;

	printf("%lld.%d.%d",mm.get_Galleon(),mm.get_Sickle(),mm.get_Knut());
	return 0;
}


运算符重载在ACM中的运用

标签:c++   acm   

原文地址:http://blog.csdn.net/u011889952/article/details/44754635

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!