以下代码可以通过,主要思路是用字符串string来实现乘法,代码如下,写得不好的地方huan'ying'pi***'zhi'zheng
#include<iostream>
#include<string>
#include<vector>
using namespace std;
string myMultiplyString(string strU, string strB) {
int numU = strU.size();
int numB = strB.size();
vector<int> mulResInt(numB+numU,0);
int numVecResInt = mulResInt.size();
int intU, intB;
for (int b = numB-1; b >= 0; b--) { // 下面的因子
for (int u = numU - 1; u >= 0; u--) { // 上面的因子
intU = strU[u] - '0';
intB = strB[b] - '0';
mulResInt[numB+numU -2-(u + b)] += intU * intB;
}
}
// 开始进位
for (int k = 0; k < numB+numU -1; k++) {
mulResInt[k + 1] += mulResInt[k] / 10;
mulResInt[k] %= 10;
}
int numVecResStr;
if (mulResInt[numVecResInt - 1] == 0)numVecResStr = numVecResInt-1;
else numVecResStr = numVecResInt;
string mulResStr(numVecResStr, '0');
// 组合成字符串
for (int k = 0; k < numVecResStr; k++) {
mulResStr[numVecResStr - k - 1] += mulResInt[k];
}
return mulResStr;
}
string mypow(string R, int n) {
string powRes = "1";
for (int i = 0; i < n; i++) {
powRes = myMultiplyString(powRes, R);
}
return powRes;
}
int mydelPoint(string &str) {
// 如果字符串小数部分末尾几位是0,则先去除
int numXiaoshu = 0;
auto pointPos = str.find('.');
if (pointPos != string::npos) {
int n = str.size();
while (str[n - 1] == '0') {
n--;
}
str.erase(str.begin() + n, str.end());
numXiaoshu = str.size() - pointPos - 1;
str.erase(str.begin() + pointPos, str.begin() + pointPos + 1);
}
return numXiaoshu;
}
void myaddPoint(string &str, int pos) {
str.insert(pos, ".");
}
int main() {
string R;
int n;
vector<string> Rvec;
vector<int> nvec;
while (cin >> R >> n) {
Rvec.push_back(R);
nvec.push_back(n);
if (cin.get() == '\n')break;
}
if (Rvec.size() != nvec.size())return 0;
int iniNumXSInt,powNumXSInt;
string iniNumXSStr;
for (int i = 0; i < Rvec.size(); i++) {
iniNumXSInt = mydelPoint(Rvec[i]);
string temp = mypow(Rvec[i], nvec[i]);
if (iniNumXSInt > 0) {
powNumXSInt = temp.size() - nvec[i] * iniNumXSInt;
myaddPoint(temp, powNumXSInt);
}
cout << temp;
}
cout << endl;
return 0;
}


京公网安备 11010502036488号