POJ 1001-Exponentiation,测试用例及本人未优化代码

该博客主要介绍了POJ 1001题目的解决方案,需要计算0.0 < R < 99.999之间实数R的n次方,其中0 < n <= 25。博主给出了未优化的C++代码,内存占用512KB,运行时间为16MS。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problems involving the computation of exact values of very large magnitude and precision are common. For example, the computation of the national debt is a taxing experience for many computer systems. 

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.

测试用例:

95.123 12
0.4321 20
5.1234 15
6.7592  9
98.999 10
1.0100 12
.00001  1
.12345  1
0001.1  1
1.1000  1
10.000  1
000.10  1
000000  1
000.00  1
.00000  0
000010  1
000.10  1
0000.1  1
00.111  1

0.0001  1
0.0001  3
0.0010  1
0.0010  3
0.0100  1
0.0100  3
0.1000  1
0.1000  3
1.0000  1
1.0000  3
1.0001  1
1.0001  3
1.0010  1
1.0010  3
1.0100  1
1.0100  3
1.1000  1
1.1000  3
10.000  1
10.000  3
10.001  1
10.001  3
10.010  1
10.010  3
10.100  1
10.100  3
99.000  1
99.000  3
99.001  1
99.001  3
99.010  1
99.010  3
99.100  1
99.100  3
99.998  1
99.998  3
注意 结果的前后零都不能有,但允许出现 .123这样的 但是不能有 1000.这样的 ,就是说小数点可以是第一位,但不能是最后一位。

代码为c++代码,比较渣,内存占用512K,时间为16MS,未优化:

#include<iostream>
#include<string>
using namespace std;

string BigMulti(string multiplied, string multiplier)
{
	int dotIndexMultiplier = multiplier.find(".");
	int dotIndexMultiplied = multiplied.find(".");
	if (dotIndexMultiplied != -1)
	{
		multiplied = multiplied.erase(dotIndexMultiplied, 1);
	}
	if ( dotIndexMultiplier !=-1){
		
		multiplier = multiplier.erase(dotIndexMultiplier, 1);
	}
	const char * multiplierString = multiplier.c_str();
	const char * multipliedString = multiplied.c_str();
	int multipliedLength = multiplied.length();
	int multiplierLength = multiplier.length();

	int realMultiplierDotIndex = multiplierLength - dotIndexMultiplier ;
	int realMultipliedDotIndex = multipliedLength - dotIndexMultiplied ;

	int realResultDotIndex = realMultipliedDotIndex + realMultiplierDotIndex;

	int sumLength = multipliedLength + multiplierLength;
	int *resultArray = new int[sumLength];
	memset(resultArray, 0, sumLength *sizeof(int));
	for (int i = 0; i < multipliedLength; i++)
		for (int j = 0; j < multiplierLength; j++)
	{
		resultArray[i + j + 1] += ((multipliedString[i] - '0')*(multiplierString[j] - '0'));
	}
	for (int resultIndex = sumLength-1; resultIndex>0; resultIndex--)
	{
		resultArray[resultIndex - 1] += resultArray[resultIndex] / 10;
		resultArray[resultIndex] = resultArray[resultIndex]% 10;
		
	}
	
	string resultString = string();

	for (int resultIndex = 0; resultIndex < sumLength; resultIndex++)
	{
		if (dotIndexMultiplied !=-1 || dotIndexMultiplier !=-1){
			if (sumLength - resultIndex == realResultDotIndex)
				resultString.push_back('.');
		}
		resultString.push_back(char('0' + resultArray[resultIndex]));
	}

	delete [] resultArray;
	return resultString;

}
string BigExponention(string base, int exp)
{
	string result = base;
	for (int i = exp; i > 1; i--)
	{
		result = BigMulti(result, base);
	}
	return result;
}
int main()
{
	//DWORD dwStart = GetTickCount(); //取windows启动到现在的流逝时间(毫秒)
	string s = string();
	int n = 0;
	while (cin >> s >> n)
	{
		string result = BigExponention(s, n);
		if (result != string("0"))
		{
		
		result.erase(0, result.find_first_not_of("0"));
		result.erase(result.find_last_not_of("0") + 1);
		}
		if (result[result.length() - 1] == '.')
		{
			result.erase(result.length()-1);
		}
		cout << result << "\n";
	}
	
	//cout << result<<"\n";
	//DWORD dwUsed = GetTickCount() - dwStart; //计算该函数所消耗的时间
	//cout << dwUsed;
}


Problems involving the computation of exact values of very large magnitude and precision are common. For example, the computation of the national debt is a taxing experience for many computer systems. 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. 输出说明 The output will consist of one line for each line of input giving the exact value of R^n. 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. 输入样例 95.123 12 0.4321 20 5.1234 15 6.7592 9 98.999 10 1.0100 12 输出样例 548815620517731830194541.899025343415715973535967221869852721 .00000005148554641076956121994511276767154838481760200726351203835429763013462401 43992025569.928573701266488041146654993318703707511666295476720493953024 29448126.764121021618164430206909037173276672 90429072743629540498.107596019456651774561044010001 1.126825030131969720661201 小提示 If you don't know how to determine wheather encounted the end of input: s is a string and n is an integer C++ while(cin>>s>>n) { ... } c while(scanf("%s%d",s,&n)==2) //to see if the scanf read in as many items as you want /*while(scanf(%s%d",s,&n)!=EOF) //this also work */ { ... } 来源 East Central North America 1988 北大OJ平台(代理
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值