SGU[112] a^b-b^a

Description

描述

You are given natural numbers a and b. Find ab-ba.

给定自然数a、b,求ab-ba

 

Input

输入

Input contains numbers a and b (1≤a,b≤100).

输入文件包含a和b(0<a,b<=100)。


Output

输出

Write answer to output.

输出答案。


Sample Input

样例输入

2 3


Sample Output

样例输出

-1

 

Analysis

分析

非常明显的高精度,再观察一下样例,要处理减法,而且有负数,注意一下好了。

 

Solution

解决方案

#include <iostream>
#include <memory.h>

using namespace std;

const int MAX = 1024;
const int HEX = 10000;
const int BIT = 4;

class Huge
{
public:
	Huge();
	Huge(int x);
	~Huge();
	
public:
	Huge& operator *= (int x);
	Huge& operator - (Huge &x);
	bool operator > (Huge x);
	
public:
	friend ostream& operator << (ostream &out, Huge &x);
	
public:
	int m_pData[MAX];
	int m_nLen;
};

Huge::Huge()
{
	memset(m_pData, 0, sizeof(m_pData));
	m_nLen = 1;
}

Huge::Huge(int x)
{
	memset(m_pData, 0, sizeof(m_pData));
	m_pData[1] = x;
	m_nLen = 1;
}

Huge::~Huge()
{
	
}

bool Huge::operator > (Huge x)
{
	if(this->m_nLen != x.m_nLen)
	{ return this->m_nLen > x.m_nLen; }
	else
	{
		for(int i = this->m_nLen; i >= 1; i--)
		{
			if(this->m_pData[i] != x.m_pData[i])
			{ return this->m_pData[i] > x.m_pData[i]; }
		}
	}
	return true;
}

Huge& Huge::operator *= (int x)
{
	for(int i = 1; i <= this->m_nLen; i++)
	{ this->m_pData[i] *= x; }
	for(int i = 1; i <= this->m_nLen; i++)
	{
		this->m_pData[i + 1] += this->m_pData[i] / HEX;
		this->m_pData[i] %= HEX;
	}
	while(this->m_pData[this->m_nLen + 1]) { this->m_nLen++; }
	return *this;
} 

Huge& Huge::operator - (Huge &x)
{
	bool bFlag = (*this > x);
	if(!bFlag) { swap(*this, x); }
	Huge *ans = new Huge();
	ans = this;
	for(int i = 1; i <= ans->m_nLen; i++)
	{
		ans->m_pData[i] -= x.m_pData[i];
		if(ans->m_pData[i] < 0)
		{
			ans->m_pData[i + 1]--;
			ans->m_pData[i] += HEX;
		}
	}
	while(!ans->m_pData[ans->m_nLen] && ans->m_nLen) { ans->m_nLen--; }
	if(!bFlag) { ans->m_pData[ans->m_nLen] = -ans->m_pData[ans->m_nLen]; }
	return *ans;
}

ostream& operator << (ostream &out, Huge &x)
{
	out << x.m_pData[x.m_nLen]; 
	for(int i = x.m_nLen - 1; i >= 1; i--)
	{ 
		if(x.m_pData[i] < 1000) { out << "0"; }
		if(x.m_pData[i] < 100) { out << "0"; }
		if(x.m_pData[i] < 10) { out << "0"; }
		out << x.m_pData[i]; 
	}
	return out;
} 

int main()
{
	int a, b;
	while(cin >> a >> b)
	{
		Huge x(a), y(b);
		for(int i = 1; i < b; i++)
		{ x *= a; }
		for(int i = 1; i < a; i++)
		{ y *= b; }
		cout << x - y << endl;
	}
	return 0;
}

  

高精度是我最惧写的,因为太麻烦,每次都要写上百行,还要重载运算符,弄不好还需要进行调试。

转载于:https://www.cnblogs.com/Ivy-End/p/4274845.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值