ACM Exponentiation resolution

Exponentiation (POJ1001 )

Time Limit: 500MS

Memory Limit: 10000K

 

Description

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 of Rn where R is a real number ( 0.0 < R < 99.999 ) and n is an integer such that 0 < n <= 25.

Input

The input will consist of a set of pairs of values for R and n. The R value will occupy columns 1 through 6, and the n value will be in columns 8 and 9.

Output

The output will consist of one line for each line of input giving the exact value of R^n. Leading zeros should be suppressed in the output. Insignificant trailing zeros must not be printed. Don't print the decimal point if the result is an integer.

Sample Input

95.123 12

0.4321 20

5.1234 15

6.7592  9

98.999 10

1.0100 12

Sample Output

548815620517731830194541.899025343415715973535967221869852721

.00000005148554641076956121994511276767154838481760200726351203835429763013462401

43992025569.928573701266488041146654993318703707511666295476720493953024

29448126.764121021618164430206909037173276672

90429072743629540498.107596019456651774561044010001

1.126825030131969720661201

Hint

If you don't know how to determine wheather encounted the end of input:
s is a string and n is an integer

C++


while(cin>>s>>n)


{


...


}


c


while(scanf("%s%d",s,&n)==2) //to  see if the scanf read in as many items as you want


/*while(scanf(%s%d",s,&n)!=EOF) //this also work    */


{


...


}

Resolution

  1 #include<stdlib.h>
  2 #include<stdio.h>
  3 #include<string.h>
  4 
  5 #define MaxLength 128
  6 
  7 typedef struct{
  8     char value[MaxLength];
  9     int pos;
 10 }High_Accuracy_Data;
 11 
 12 int numlength(High_Accuracy_Data *n)
 13 {
 14     for (int i=0;i<MaxLength;i++)
 15     {
 16         if (n->value[i]==-1)
 17         return i;
 18     }
 19     return 0;
 20 }
 21 
 22 void MultipTwo(High_Accuracy_Data *OUTData, High_Accuracy_Data INData)
 23 {
 24     int i=0,j=0;
 25     int nlengthOUT=numlength(OUTData);
 26     int nlengthIN=numlength(&INData);
 27     High_Accuracy_Data c;
 28 
 29     //Clear the buffer
 30     c.pos=0;
 31     for (i=0;i<MaxLength;i++)
 32     {
 33         c.value[i]=0;
 34     }
 35 
 36     //Caculate the two number multiplication
 37     for(i=0;i<nlengthIN;i++)
 38     {
 39         for(j=0;j<nlengthOUT;j++)
 40         {
 41             c.value[i+j]+=(OUTData->value[j])*(INData.value[i]);
 42 
 43             //eg..9*9 = 81
 44             //Get the High char 8
 45             c.value[i+j+1]+=c.value[i+j]/10;
 46 
 47             //Get the low char 1
 48             c.value[i+j]%=10;
 49         }
 50     }
 51 
 52     for (i=nlengthOUT+nlengthIN;;i--)
 53     {
 54         if (c.value[i-1]!=0)
 55         {
 56             c.value[i]=-1;
 57             break;
 58         }
 59     }
 60 
 61     //Caculate the point
 62     c.pos=OUTData->pos+INData.pos;
 63     OUTData->pos=c.pos;
 64 
 65     //Output
 66     for (i=0;c.value[i]!=-1;i++)
 67     {
 68         OUTData->value[i]=c.value[i];
 69     }
 70     OUTData->value[i]=c.value[i];
 71 }
 72 
 73 High_Accuracy_Data *createRBuffer(char* RBuffer)
 74 {
 75     High_Accuracy_Data *pData=(High_Accuracy_Data *)malloc(sizeof(High_Accuracy_Data));
 76 
 77     int len=strlen(RBuffer);
 78     int i=0;
 79     int bContainPoint = 0;
 80 
 81     //clear the buffer
 82     pData->pos=0;
 83     for (i=0;i<MaxLength;i++)
 84     {
 85         pData->value[i]=0;
 86     }
 87 
 88     for (i=len-1;i>=0;i--)
 89     {
 90         if (RBuffer[i]=='.')
 91         {
 92             bContainPoint=1;
 93             pData->pos=len-i-1;
 94         }else
 95         {
 96             pData->value[len-i-1-bContainPoint]=RBuffer[i]-'0';
 97         }
 98     }
 99     for (i=len-bContainPoint;i>=0;i--)
100     {
101         if (pData->value[i-1]!=0)
102         {
103             pData->value[i]=-1;
104             break;
105         }
106     }
107     return pData;
108 }
109 
110 void printnum(High_Accuracy_Data n)
111 {
112     int len=numlength(&n);
113     int i,end=0,b=0;
114     if (n.pos>=len){
115         printf(".");
116         for (i=n.pos-len;i>0;i--)
117             printf("0");
118     }
119     if (n.pos!=0){
120        for (i=0;i<n.pos;i++)
121            if (n.value[i]!=0) {
122                end=i;
123                b=1;
124                break;
125            }
126        if (end==0&&b==0) end=n.pos;
127     }
128     for (i=len-1;i>=end;i--){
129         printf("%d",n.value[i]);
130         if (i==n.pos&&i!=end) printf(".");
131     }
132     printf("\n");
133 }
134 
135 int main()
136 {
137     //base-number
138     char RBuffer[7];
139     //exponential
140     int exp;
141     //return value for the high accuracy data
142     High_Accuracy_Data *InputData=NULL,*OutPutData=NULL;
143 
144     while(scanf("%s%d",RBuffer,&exp)!=EOF)
145     {
146         //95.123 12 is input value format,
147         //the last place put a '\0', no matter what the
148         //input is.
149         RBuffer[6]='\0';
150 
151         //Clean the buffer
152         if (InputData!=NULL) free(InputData);
153         if (OutPutData!=NULL) free(OutPutData);
154 
156         InputData=createRBuffer(RBuffer);
157         OutPutData=createRBuffer(RBuffer);
158 
159         for (int i=0;i<exp-1;i++)
160         {
161             MultipTwo(OutPutData,*InputData);
162         }
163         printnum(*OutPutData);
165         //Used for eclipse debug
166         fflush(stdout);
168     }
169     return 0;
170 }

 

posted @ 2012-04-06 17:47  SolidMango  阅读(761)  评论(0编辑  收藏  举报