http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=25197
Given a floating-point number A, the exact value of A's n-th square is omitted, and the leading 0 and the extra 0 are ignored.
Use a struct to hold floating-point number a minus the value of the decimal point, record the number of decimal places, and then use the multiplication of large numbers instead of the N power of a to find the exact value.
#include <cstdio> #include <cstring>struct node{int len; int num[1000]; Node () {memset (num,0,sizeof (num)); len=0;} The constructor automatically executes each time a struct variable is defined};node solve (node X1,node x2)//large number multiplication {int i,j; Node X3; for (i=0;i<x1.len;i++) {for (j=0;j<x2.len;j++) {x3.num[i+j]=x1.num[i]*x2.num[j]+x3.num[i +J]; X3.NUM[I+J+1]=X3.NUM[I+J+1]+X3.NUM[I+J]/10; x3.num[i+j]=x3.num[i+j]%10; }} I=x1.len+x2.len; while (!x3.num[i]) i--; x3.len=i+1; Note To determine the number of digits after the multiply return x3;} int main () {//freopen ("A.txt", "R", stdin); Char s[100],ss[100]; int n,i,j; while (~SCANF ("%s%d", s,&n)) {int L=strlen (s); int ans=0,m=0; Node p1,p2; memset (ss, ' n ', sizeof (SS)); j=0; for (i=0;i<l;i++) {if (s[i]!= '. ') ss[j++]=s[i]; Remove the decimal point else ans=l-i-1; Also record the position of the decimal point} for (i=0;i<j;i++) {p1.num[p1.len++]=ss[j-i-1]-' 0 ';The array is converted to a string} P2=P1; struct copy shallow copy for (i=1;i<n;i++)//p n Power {p2=solve (P1,P2); } i=l*n; The maximum number of ans*=n; Number of decimal places//printf ("%d%d\n", ans,i); while (!p2.num[i]&&i>=ans) i--; Determines the position of the decimal point if (i==ans-1) {printf ("."); for (j=0;j<i;j++)//Ignore trailing extra 0 if (p2.num[j]) break; for (; i>=j;i--) {printf ("%d", p2.num[i]); } printf ("\ n"); } else {for (j=0;j<i;j++) if (p2.num[j]) break; for (; i>=j;i--) {if (i==ans-1) printf ("."); printf ("%d", p2.num[i]); } printf ("\ n"); }} return 0;}
UVA-748 exponentiation