2011-11-23
高精度浮点数计算。
花了我整整一天时间才写好,POJ上给的测试数据都通过,0的1次方和1的0次方之类的输入和加入一些干扰0的输入测试也都通过,但是提交后AC不了,显示Anwser Wrong,不知道什么问题,也没找出来。崩溃了。
好好练练吧,鉴于道理已明了,先草草结贴,有时间再看。
?
?
#include <iostream> #include <vector> #include <string> using namespace std; void getResult(vector< vector<int> >&, string, int); void take0(string&); void toIntVector(vector<int>&, const string&); void mul(vector<int>&, vector<int>&); void takeVetor0(vector<int>&); void addDot(vector<int>&, int); int main () { vector< vector<int> > vvResult; string strTmp; int iIndex; while (cin>>strTmp>>iIndex) { getResult(vvResult, strTmp, iIndex); // show result string strResult = ""; vector< vector<int> >::iterator itervvResult = vvResult.begin(); while (itervvResult != vvResult.end()) { vector<int>::iterator iterI = (*itervvResult).begin(); //cout << "Final viPro: "; while (iterI != (*itervvResult).end()) { if (*iterI == 10) { strResult += "."; } else { strResult += ('0' + *iterI); } iterI++; } cout << strResult << endl; itervvResult++; } vvResult.clear(); } return 0; } void getResult(vector< vector<int> >& vvResult, string strBase, int iIndex) { take0(strBase); int iPos = -1; int iBase = 0; while (iBase < strBase.size()) { if (strBase[iBase] == '.') { iPos = iBase; } iBase ++; } if (iPos == -1) { strBase += "."; } int iDotLocate = (strBase.size() - 1 - strBase.find(".")) * iIndex; strBase.erase(strBase.find("."), 1); take0(strBase); //cout << "take0: " << strBase << " Dot:" << iDotLocate << endl; vector<int> viBase; toIntVector(viBase, strBase); vector<int> viPro(viBase); if (iIndex == 0) { viPro.clear(); viPro.assign(1, 1); vvResult.push_back(viPro); return; } else if (strBase == "0" || strBase == ".") { viPro.clear(); vvResult.push_back(viPro); return; } while (--iIndex) { mul(viPro, viBase); } addDot(viPro, iDotLocate); vvResult.push_back(viPro); } void take0(string &strBase) { while('0' == strBase[strBase.size() -1] && 1 != strBase.size()) { strBase.erase(strBase.size() -1, 1); } while('0' == strBase[0] && 1 != strBase.size()) { strBase.erase(0, 1); } } void toIntVector(vector<int>& viBase, const string& strBase) { int i = 0; while (i < strBase.size()) { viBase.push_back(strBase[i++] - '0'); } } void mul(vector<int>& viPro, vector<int>& viBase) { int i = 0; int j = 0; vector<int> viTmpResult(512, 0); // calculate int iCount; for (i = 0; i < viBase.size(); i++) { for (j = 0; j < viPro.size(); j++) { viTmpResult[i + j] += viPro[j] * viBase[i]; iCount = i + j; } } viPro.clear(); viPro.assign(512, 0); // handle carry i = i -1 + j -1; j = 0; while (i >= 0) { viPro[j] += viTmpResult[i]; if (viPro[j] /10 > 0) { viPro[j+1] += viPro[j] / 10; viPro[j] %= 10; } i--; j++; } while (viPro[j] != 0) { viPro[j+1] += viPro[j] / 10; viPro[j] %= 10; j++; } // reverse vi i = 0; j -= 1; //cout <<"i, j "<< i << " "<< j-i <<endl; while (i < j-i) { viPro[i] = viPro[i] + viPro[j - i]; viPro[j - i] = viPro[i] - viPro[j - i]; viPro[i] = viPro[i] - viPro[j - i]; i++; } //cout <<"i, j "<< i -1 << " "<< j-i +1 <<endl; // erase 0 takeVetor0(viPro); } void takeVetor0(vector<int>& vi) { vector<int>::iterator iterI = vi.end(); iterI--; while (*iterI == 0) { iterI--; } iterI++; if (iterI != vi.end()) { vi.erase(iterI, vi.end()); } } void addDot(vector<int>& vi, int iDot) { //cout << "iDot: " << iDot << endl; if (iDot == 0) { return; } vector<int>::iterator iterI = vi.begin(); if (vi.size() < iDot) { vi.insert(iterI, iDot - vi.size(), 0); vi.insert(vi.begin(), 10); } else { vi.insert(iterI + (vi.size() - iDot), 10); } }?
?