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
#include
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;
}