poj[1001] Exponentiation

本文介绍了一种实现特定格式浮点数乘方运算的C++算法。该算法能够处理包含小数部分的数字,并通过逐位相乘的方式完成乘方计算。文章详细展示了如何解析输入字符串、进行乘法操作并输出最终结果。
#include<iostream>
#include<string>

using namespace std;

int main(int argc, char* argv[])
{
	string r;
	int n, dian;
	const int len = 160;
	short result[len], jieguo[len], chengshu[6];
	while (cin>>r>>n)
	{
		int len1 = 0;
		for (int i = 0; i < len; ++i)jieguo[i] = result[i] = 0;
		for (int i = 0; i < 6; ++i)chengshu[i] = 0;
		dian = 0;
		string::size_type pos = r.find(".");
		if (pos != string::npos)dian = (5 - pos)*n;
		for (int i = 5, j = 0; i >= 0; --i)
		{
			if (r[i] != '.')
			{
				jieguo[j] = result[j] = chengshu[j] = r[i] - '0';
				++j;
			}
		}
		while (n>=2)
		{
			--n;
			for (int i = 0; i < len; ++i)result[i] = 0;
			for (int i = 0; i < 5; ++i)
			{
				int c;
				for (int j = 0; j < len; ++j)
				{
					if (chengshu[i] == 0)break;
					c = chengshu[i] * jieguo[j];
					result[i + j] += c;
					for (int t = i + j; result[t] > 9; ++t)
					{
						result[t + 1] += result[t] / 10;
						result[t] = result[t] % 10;

					}
				}
			}
			for (int i = 0; i < len; ++i) jieguo[i]=result[i];

		}
		/*int firstindex = -1;
		for (int i = len - 1; i >= dian; --i)
		{
			if (result[i] > 0)
			{
				firstindex = i;
				break;
			}
		}
		int lastindex = -1;
		for (int i = 0; i < dian; ++i)
		{
			if (result[i] > 0)
			{
				lastindex = i;
				break;
			}
		}
		if (firstindex != -1)
		{
			while (firstindex >= dian)
			{
				cout << result[firstindex];
				--firstindex;
			}
			
		}
		if (lastindex != -1)
		{
			cout << '.';
			--dian;
			while (dian>=lastindex)
			{
				cout << result[dian];
				--dian;
			}
		}
		cout << endl;
		*/
		int firstindex = -1;
		for (int i = len - 1; i >= dian; --i)
		{
		  if (jieguo[i] > 0)
		   {
		     firstindex = i;
		     break;
		   }
		}
		int lastindex = -1;
		for (int i = 0; i < dian; ++i)
		{
		  if (jieguo[i] > 0)
		  {
		     lastindex = i;
		     break;
		   }
		}
		if (firstindex != -1)
		{
		   while (firstindex >= dian)
		   {
		     cout << jieguo[firstindex];
		     --firstindex;
		    }

		}
		if (lastindex != -1)
		{
		cout << '.';
		--dian;
		while (dian>=lastindex)
		{
		cout << jieguo[dian];
		--dian;
		}
		}
		cout << endl;
		


	}

	return 0;
}



参见:http://blog.youkuaiyun.com/shaw1994/article/details/12273987

POJ1001Exponentiation)是一道经典的编程题目,要求实现一个高精度的幂运算。输入包括一个浮点数 R(基数)和一个整数 n(指数),输出是 R 的 n 次方的结果,并且结果需要去除末尾的无效零和可能的无效小数点[^1]。下面是一个使用 C++ 编写的示例解决方案,它利用字符串处理和大数乘法来实现高精度计算。 ### 核心思路 1. **输入处理**:读取浮点数 `R` 和整数 `n`。 2. **小数点处理**:将 `R` 转换为整数形式,计算小数点后的位数,并调整最终结果的小数位数。 3. **快速幂乘法**:使用大数乘法实现幂运算。 4. **结果处理**:去除末尾的无效零,输出格式化的结果。 ### C++ 示例代码 ```cpp #include <iostream> #include <string> #include <algorithm> using namespace std; // 大数乘法函数 string multiply(const string &a, const string &b) { string result(a.size() + b.size(), '0'); for (int i = a.size() - 1; i >= 0; --i) { for (int j = b.size() - 1; j >= 0; --j) { int product = (a[i] - '0') * (b[j] - '0') + (result[i + j + 1] - '0'); result[i + j + 1] = (product % 10) + '0'; result[i + j] += product / 10; } } // 去除前导零 size_t start_pos = result.find_first_not_of('0'); if (start_pos != string::npos) { result = result.substr(start_pos); } else { return "0"; } return result; } // 去除末尾的零和无效小数点 string formatResult(const string &s, int decimalDigits) { string result = s; if (decimalDigits > 0) { if (result.size() <= decimalDigits) { result.insert(0, decimalDigits + 1 - result.size(), '0'); result.insert(0, "0."); } else { result.insert(result.size() - decimalDigits, "."); } } // 去除末尾无效零 result.erase(result.find_last_not_of('0') + 1); if (result.back() == '.') { result.pop_back(); } return result; } int main() { string r; int n; while (cin >> r >> n) { // 处理输入中的小数点 int decimalDigits = 0; size_t dotPos = r.find('.'); if (dotPos != string::npos) { decimalDigits = r.size() - dotPos - 1; r.erase(dotPos, 1); } // 计算新的小数位数 decimalDigits *= n; // 快速幂计算 string result = "1"; for (int i = 0; i < n; ++i) { result = multiply(result, r); } // 格式化输出结果 cout << formatResult(result, decimalDigits) << endl; } return 0; } ``` ### 代码说明 - **大数乘法**:`multiply` 函数实现了两个字符串表示的大数相乘,并返回结果字符串。 - **结果格式化**:`formatResult` 函数处理最终结果,去除末尾的零并正确放置小数点。 - **输入处理**:代码通过字符串操作将输入中的小数点去除,并调整最终结果的小数位数。 ### 示例输入输出 输入: ``` 95.123 12 ``` 输出: ``` 548815620517731830194541.899025343415715973535967221869852721 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值