POJ 1001 Exponentiation

参考了《Programming_guide_and_online_practice-4.23bylwx》的第七章大整数计算,解题思路是先将浮点数转化成大整数进行计算,再算出小数的位数,最后打印输出。
POJ上题目中给的数据都能测试通过,但提交是Wrong Answer,可能是没考虑特殊的输入。这里仅贴出代码,供参考。

#include <iostream> #include <string.h> #define MAX_LEN 102 using namespace std; unsigned aZheng[MAX_LEN+10]; unsigned aResZheng[MAX_LEN+10]; unsigned tmp[MAX_LEN+10]; int main(int argc, char* argv[]) { int n; char szR[7]; memset(szR, 0, sizeof(szR)); while (cin >> szR >> n) { int i,j,p; int nLenZheng, nLenXiao; p = 0; while (szR[p] != 0) if (szR[++p] == '.') break; nLenZheng = p; nLenXiao = 5 - nLenZheng; cout << nLenZheng << " " << nLenXiao << endl; j = 0; for (i = 5; i >= 0; i--) if (szR[i] != '.') aZheng[j++] = szR[i]-'0'; //相乘 memset(aResZheng, 0, sizeof(aResZheng)); for (j = 0; j < 5; j++) aResZheng[j] = aZheng[j]; int nTimes = n; while (--nTimes > 0) { for (i = 0; i < MAX_LEN; i++) tmp[i] = aResZheng[i]; memset(aResZheng, 0, sizeof(aResZheng)); for (i = 0; i < MAX_LEN; i++) for (j = 0; j < 5; j++) aResZheng[i+j] += tmp[i]*aZheng[j]; //进位 for (i = 0; i < MAX_LEN; i++) { if (aResZheng[i] >= 10) { aResZheng[i+1] += aResZheng[i]/10; aResZheng[i] %= 10; } } } //输出 p = MAX_LEN; while (aResZheng[--p] == 0); int nResXiaoLen = nLenXiao * n; for (i = p; i >=nResXiaoLen; i--) cout << aResZheng[i]; cout << "."; p = 0; while (aResZheng[++p] == 0); for (i = nResXiaoLen-1; i >= p; i--) cout << aResZheng[i]; memset(szR, 0, sizeof(szR)); } return 0; }

你可能感兴趣的:(POJ 1001 Exponentiation)