编程语言
首页 > 编程语言> > C++ ! 浮点数的n次幂计算 (大数)!

C++ ! 浮点数的n次幂计算 (大数)!

作者:互联网

This problem requires that you write a program to compute the exact value of Rn where R is a real number ( 0.0 < R < 99.999 ) and n is an integer such that 0 < n ≤ 25.

输入
The input will consist of a set of pairs of values for R and n. The R value will occupy columns 1 through 6, and the n value will be in columns 8 and 9.

95.123 12
0.4321 20
5.1234 15
6.7592  9
98.999 10
1.0100 12

输出
The output will consist of one line for each line of input giving the exact value of Rn. Leading zeros should be suppressed in the output. Insignificant trailing zeros must not be printed. Don’t print the decimal point if the result is an integer.

548815620517731830194541.899025343415715973535967221869852721
0.00000005148554641076956121994511276767154838481760200726351203835429763013462401
43992025569.928573701266488041146654993318703707511666295476720493953024
29448126.764121021618164430206909037173276672
90429072743629540498.107596019456651774561044010001
1.126825030131969720661201
#include<string>
#include<vector>
#include<iostream>
using namespace std;
string ToString(int n)
{
	char ch = '0';
	ch += n;
	string str;
	str += ch;
	return str;
}

string MultiplyStringInt(string nStr, string mStr)
{
    if (nStr == "0" || mStr == "0"){
		return "0";
	}
	vector<int> multVec(nStr.size()+mStr.size()-1,0);
	if (nStr.size() < mStr.size()){
		swap(nStr, mStr);
	}
	for (int i = 0; i< mStr.size(); i++)
	{
		int mInt = mStr[i] - '0';
		for(int j = 0; j< nStr.size(); j++)
		{
			int nInt = nStr[j] - '0';
			multVec[i+j] += (mInt * nInt);
		}
	}
	string result;
	while (!multVec.empty())
	{
		int back = multVec.back();
		result.insert(0, ToString(back % 10));
		multVec.pop_back();
		if(multVec.empty()){
			if(back >9){
				result.insert(0, ToString(back / 10));
			}
		} else {
			back /= 10;
			multVec.back() += back;
		}
	}
	return result;
}
int DealPoint(string &numStr)
{
	int point = 0;
	auto pointSite = numStr.find('.');
	if (pointSite != string::npos){
		numStr.erase(numStr.begin() + pointSite);
		point = numStr.size() - pointSite;
		// **************清零******************
		while (!numStr.empty() && numStr.back() == '0')
		{
			numStr.erase(numStr.end() - 1);
			point--;
		}
		while (!numStr.empty() && numStr[0] == '0')
		{
			numStr.erase(numStr.begin());
		}
		// **************清零******************	
	}
	return point;
}
string Pow(string numStr, int time)
{
	string powRes = "1";
	while (time-- > 0)
	{
		powRes = MultiplyStringInt(powRes, numStr);
	}
	return powRes;
}
void AddPoint(string &powRes,int pointSite)
{
	if(powRes.size() <= pointSite){
		powRes = string(pointSite - powRes.size() + 1, '0') + powRes;
	}
	powRes.insert(powRes.size() - pointSite, ".");
}
int main(){
	string numStr;
	int powTime;
	while(cin >> numStr >> powTime){
		int pointSite = DealPoint(numStr);
		string powRes = Pow(numStr, powTime);
		AddPoint( powRes, pointSite * powTime);
		cout<< powRes<<::endl;
	}
	return 0;
}

标签:numStr,string,大数,int,浮点数,back,C++,powRes,size
来源: https://blog.csdn.net/weixin_42228705/article/details/100034478