POJ Exponentiation解题
Problem: 1001 | User: Quincy | |
Memory: 260K | Time: 0MS | |
Language: C++ | Result: Accepted |
- Source Code
// ExponentiationDemo.cpp : Defines the entry point for the console application. // #include <string.h> #include <iostream> using namespace std; const int DATA_LENGTH=200; const int INPUT_LENGTH=100; struct LargeNumber { int dotIndex; int dataLength; char data[DATA_LENGTH]; }; struct InputNumber { LargeNumber number; int power; }; void MultiLargeNumber(const LargeNumber* num1, const LargeNumber* num2, LargeNumber* outResult) { int nLength1 = num1->dataLength; int nLength2 = num2->dataLength; LargeNumber tempValue; memset(&tempValue, 0, sizeof(tempValue)); int ntempValueIndex = 0; for (int i=nLength1-1; i >=0; --i, ntempValueIndex++) { int nCarryNum = 0; int index = 0; for (int j=nLength2-1; j >= 0; --j, ++index) { int nData = num1->data[i]; nData = nData * num2->data[j] + nCarryNum + tempValue.data[index+ntempValueIndex]; tempValue.data[index+ntempValueIndex] = nData % 10; nCarryNum = nData / 10; } if (0!=nCarryNum) { tempValue.data[index+ntempValueIndex] = nCarryNum; index++; } tempValue.dataLength = index + ntempValueIndex; } tempValue.dotIndex = num1->dotIndex + num2->dotIndex; /// Reserve Result to num1; memset(outResult, 0, sizeof(LargeNumber)); int nLengthResult = tempValue.dataLength; for (int i=0; i < nLengthResult; ++i) { outResult->data[i] = tempValue.data[nLengthResult-i-1]; } outResult->dotIndex = tempValue.dotIndex; outResult->dataLength = tempValue.dataLength; } bool ConvertStrToNum(const string& str, LargeNumber* num) { int ndataIndex = 0; bool bZero = true; for(int i=0; i<str.length(); ++i) { char tempValue = str.at(i); if (tempValue == '.') { num->dotIndex = str.length() - i - 1; continue; } if (tempValue < '0' || tempValue > '9') { return false; } if (tempValue != '0') { bZero = false; } num->data[ndataIndex++] = tempValue - '0'; } if (bZero) { num->dataLength = 0; num->dotIndex = 0; } else { num->dataLength = ndataIndex; } return true; } void PrintLargeNumber(const LargeNumber* num) { char szResult[DATA_LENGTH] = {0}; int nResultIndex = 0; int nLength = num->dataLength; for (int i=0; i<nLength; ++i) { if (num->dotIndex == nLength-i) { szResult[nResultIndex++] = '.'; //strResult.push_back('.'); //cout << "." ; } szResult[nResultIndex++] = char(num->data[i]+'0'); //strResult.push_back(char(num->data[i]+'0')); //cout << char(num->data[i]+'0'); } /// Remove back zero for (int i = nResultIndex-1; i >= 0; --i) { if (szResult[i] != '0') { if (szResult[i] == '.') { szResult[i] = 0; } break; } szResult[i] = 0; } /// Remove front zero int nStartPos = 0; for (; nStartPos < nResultIndex; ++nStartPos ) { if (szResult[nStartPos] != '0') { break; } } cout << (char*)(szResult+nStartPos) << endl; } int main() { char s[8]; int n; //cin.get(s, 7); //cin >> n; InputNumber Inputs[INPUT_LENGTH] = {0}; int nInputCount = 0; while(cin >> s >> n) { if (!ConvertStrToNum(s, &Inputs[nInputCount].number)) { return 0; //continue; } Inputs[nInputCount].power = n; ++nInputCount; } int index=0; while (index < nInputCount) { LargeNumber Result; memcpy(&Result, &Inputs[index].number, sizeof(LargeNumber)); for (int i=0; i<Inputs[index].power-1; ++i) { MultiLargeNumber(&Inputs[index].number, &Result, &Result); } PrintLargeNumber(&Result); ++index; } return 0; }
之前在遍历文件夹下的所以文件和文件夹时,我采用的是递归调用的方式。后来发现可以使用非递归的广度优先算法来实现,感觉比自己的代码要好,更优雅些。
因为我很多时候解决问题只是把它解决了就好,没有考虑更换思路,所以决定提高自己的算法方面的知识了,以给自己不同的解决思路。
开始提交几次都出问题了,后来发现还是在现实时把10.0000显示成10.而不是10所以出问题了,主要还是测试用例不全面。