POJ 1001. Exponentiation
原题链接
简单
作者:
Hanasaki
,
2021-10-12 12:52:46
,
所有人可见
,
阅读 164
#include <iostream>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;
string ans(string src, string another)
{
reverse(src.begin(), src.end());
reverse(another.begin(), another.end());
int n = src.length() + another.length();
vector<int> temp;
temp.resize(n);
for (int i = 0; i < temp.size(); i ++) temp.at(i) = 0;
for (int i = 0; i < src.length(); i ++)
{
for (int j = 0; j < another.length(); j++)
{
int tem = (src.at(i) - '0') * (another.at(j) - '0');
temp.at(i + j) += tem;
if (temp.at(i + j) > 9 && (i + j) < n - 1)
{
temp.at(i + j + 1) += temp.at(i + j) / 10;
temp.at(i + j) = temp.at(i + j) % 10;
}
else if (temp.at(i + j) > 9 && (i + j) == n - 1)
{
temp.push_back(temp.at(i + j) / 10);
temp.at(i + j) = temp.at(i + j) % 10;
}
temp.at(0) = temp.at(0);
}
}
string returnString;
returnString.resize(n);
for (int i = 0; i < n; i ++) returnString[i] = temp[i] + '0';
reverse(returnString.begin(), returnString.end());
int noZeroPos = returnString.find_first_not_of('0');
if (noZeroPos != string::npos) returnString.erase(0, noZeroPos);
else returnString = "0";
return returnString;
}
void outPut(string a, int n) {
string answerString;
int dotPos = -1;
int zeroNumble = 0;
int integerNumble;
int decimalNumble;
dotPos = a.find_first_of('.');
if (dotPos != string::npos) {
a.erase(dotPos, 1);
}
integerNumble = dotPos;
decimalNumble = a.size() - integerNumble;
zeroNumble = a.find_first_not_of('0');
answerString.push_back('1');
for (int i = 0; i < n; i ++) {
answerString = ans(answerString, a);
}
int ansSize = answerString.size();
int temp = decimalNumble * n;
temp = temp;
if (ansSize > decimalNumble * n) {
int t = answerString.size() - n * decimalNumble;
answerString.insert(ansSize - decimalNumble * n, 1, '.');
int lastZeroPos = answerString.find_last_not_of('0');
if (lastZeroPos < answerString.size() - 1) {
answerString.erase(lastZeroPos + 1, answerString.size() - 1 - lastZeroPos);
}
if (lastZeroPos == t) answerString.erase(lastZeroPos, 1);
cout << answerString;
}
else
{
cout << '.';
for (int i = 0; i < decimalNumble * n - ansSize; i ++) cout << '0';
int lastZeroPos = answerString.find_last_not_of('0');
if (lastZeroPos < answerString.size() - 1) answerString.erase(lastZeroPos + 1, answerString.size() - 1 - lastZeroPos);
int t = answerString.size() - n * decimalNumble - 1;
if (lastZeroPos == t) answerString.erase(lastZeroPos, 1);
cout << answerString;
}
cout << endl;
}
int main()
{
string a;
string in;
int n;
while (getline(cin, in))
{
if (in.size() == 0) continue;
string a = in.substr(0, 6);
int n = atoi(in.substr(7, 2).c_str());
outPut(a, n);
}
return 0;
}