poj 1001 "求高精度幂"

0MS, 364K
这是一个求高精度幂的题。思路是把所有的输入转化成整数,记录下小数点所在的位置来控制输出。

#include <cstdlib> #include <cstdio> #include <cstring> #define LEN 125 using namespace std; void function() { char s[6] = {0}; int n = 0; int bit, x, carry = 0, len, zeroBit, sum; int flag = 0; char ret[LEN] = {0}; while (scanf("%s%d", s, &n) != EOF) { bit = 0; zeroBit = 0; flag = 0; for (int i=5; i>=0; --i) { if (!flag && s[i] == '0') ++zeroBit; else { flag = 1; if (s[i] != '.') ++bit; else break; } } x = 0; for (int i=0; i<=5; ++i) if (s[i] != '.') x = x*10 + s[i] - '0'; ret[0] = '1'; sum = 0; carry = 0; for (int i=0; i<n; ++i) { len = strlen(ret); int j; for (j=0; j<len; ++j) { sum = (ret[j]-'0') * x + carry; if (sum > 9) { ret[j] = sum % 10 + 48; carry = sum / 10; } else { ret[j] = sum % 10 + 48; carry = 0; } } while (carry > 0) { ret[j] = carry % 10 + 48; carry = carry / 10; ++j; }; } len = strlen(ret); int d = len - (bit+zeroBit)*n; if (d <= 0) { putchar('.'); for (int i=d; i<0; ++i) putchar('0'); } for (int i=len-1; i>=0; --i) { if (i == zeroBit*n-1) break; if (i == (bit+zeroBit)*n-1) putchar('.'); putchar(ret[i]); } printf("/n"); memset(ret, 0, LEN); } return; } int main() { function(); return 0; }

你可能感兴趣的:(function)