zoukankan      html  css  js  c++  java
  • 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;
    }
    

      

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

  • 相关阅读:
    e667. 在给定图像中创建缓冲图像
    e661. 确定图像中是否有透明像素
    e673. Getting Amount of Free Accelerated Image Memory
    e663. 在gif图像中获取透明和色彩的数量
    e662. 取的图像的色彩模型
    e675. 翻转缓冲图像
    e665. 在图像中过滤三元色
    e679. 浮雕化图像
    e669. 绘制缓冲图像
    e664. 在图像中获取子图像
  • 原文地址:https://www.cnblogs.com/Ivy-End/p/4274845.html
Copyright © 2011-2022 走看看