Uva 748 – Exponentiation(大数相乘)
Exponentiation
Time limit: 3.000 seconds
Problems involving the computation of exact values of very large magnitude and precision are common. For example, the computation of the national debt is a taxing experience for many computer systems.
This problem requires that you write a program to compute the exact value ofRnwhereRis a real number (0.0 <R< 99.999) andnis an integer such that.
Input
The input will consist of a set of pairs of values forRandn. TheRvalue will occupy columns 1 through 6, and thenvalue will be in columns 8 and 9.
Output
The output will consist of one line for each line of input giving the exact value ofRn. Leading zeros and insignificant trailing zeros should be suppressed in the output.
Sample Input
95.123 120.4321 205.1234 156.7592 998.999 101.0100 12
Sample Output
548815620517731830194541.899025343415715973535967221869852721.0000000514855464107695612199451127676715483848176020072635120383542976301346240143992025569.92857370126648804114665499331870370751166629547672049395302429448126.76412102161816443020690903717327667290429072743629540498.1075960194566517745610440100011.126825030131969720661201
Miguel A. Revilla2000-02-09测试数据:input:
0.0110 11000. 21000 410110.0 110.1
output:
.0111000000100000000000010101
#include<stdio.h>#include<string.h>#include<time.h>#define MAXN 1000int temp[MAXN];int temple[MAXN];int patience[MAXN];int sum[MAXN], large = 0;char last[MAXN];int add(int len, int level){ i, j, e, item;for(i=0,e=0; i<len; ++i){item = (e + temple[i] + sum[i+level]);sum[i+level] = item%10;e = item/10;}for(; e!=0; ++i){item = e + sum[level+i];sum[i+level] = item%10;e = item/10;}if(i+level > large) large = i+level;}int multi(int n, int len){i, j, e, cnt, strlen, item, t, m, t_n = n-1; // t_n的值为指数减1,香港服务器租用,统计需要相乘的次数 memset(sum, 0, sizeof(sum));memset(temple, 0, sizeof(temple));large = len;while(t_n–){strlen = large;for(i=0; i<len; ++i){// 一次for循环完成时,两个大数相乘的目的达到 item = temp[i];for(j=0, e=0; j<strlen; ++j){m = item*patience[j];temple[j] = (e + m)%10;e = (e + m)/10;}cnt = strlen;if(e) temple[j] = e, cnt++;add(cnt, i);memset(temple, 0, sizeof(temple));}memset(patience, 0, sizeof(patience));for(i=0; i<large; ++i) patience[i] = sum[i];memset(sum, 0, sizeof(sum)); // 每次将相乘得到的结果赋值给patience时将sum归零 }return 0;}int main(){clock_t begin, end;int i, j, k, cnt, t, len, m, high, low;char input[10], *str;int n;begin = clock();, input, &k) != EOF){len = strlen(input);m = -1;)) != NULL)m = len – 1 – (str – input); // m 的作用是计算小数点后面有几位小数,无小数点时m=-1 memset(temp, 0, sizeof(temp));memset(patience, 0, sizeof(patience));for(i=len-1,n=0; i>=0; –i)) temp[n++] = patience[n] = input[i] – ;if(m != -1) len–;if(k>=2) multi(k, len);{high = len-1, low = 0;// high 和 low 两个变量主要是消除前缀零和尾数零的情况, 同时,若存在小数点(i=; ++i); low = i;if(m != -1){; –i); high = i;) high–;}, input[i]);printf();continue;}if(m == -1) // 处理无小数点的情况,而这时的尾数零不用消去 {low = 0, high = large – 1;for(i=large-1; patience[i] == 0; –i); high = i;, patience[i]);printf();}else{// 当存在小数点时,必须先将小数点放入大数内才能开始消去前缀零和尾数零 cnt = 0;t = m*k; // 计算最后一共有几个小数,没有先处理尾数零的其中原因之一就是避免不能准确计算t memset(last, (i=0; i<t; ++i){sprintf(last+cnt, , patience[i]); // sprintf函数将整型转换成了字符放入了字符串中 cnt++;}last[cnt++] = ; (; i<large; ++i){sprintf(last+cnt, , patience[i]);cnt++;}last[cnt] = ;len = strlen(last);low = 0, high = len-1;; i–); high = i; (i=; i++); low = i;) low++;, last[i]);printf();}}end = clock();printf(, end – begin);return 0;}
解题思路:
先将浮点数转换成整型,美国服务器,同时统计小数点后面数得个数,再进行大数相乘 1y
posted on
,香港服务器没有天生的信心,只有不断培养的信心。