Exponentiation
Time Limit: 1000/500 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2475 Accepted Submission(s): 669
Problem 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.
感覺這種大數處理的題目越來越多了,但是也是越來越喜歡這種進位的算法了,還有數組,字符等的處理,感覺以后也會越來越熟練的。下面是我根據網上的代碼做出了相應的詳解,也是越來越了解該類的編程思維了吧。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int num[10], result[200], len, flag = 0, cnt1;
int char_to_int( char *s ) /*將字符型數組s中的數據復制到整型數組num中,且返回小數位數*/
{
int i, dian = 0, j, jihao;
memset(num,0,10*sizeof(int)); //將數組num內的各個數據設為0
len = strlen(s); //len為字符數組s的長度
//printf( "%s\n", s );
//printf( "%d\n", len );
cnt1 = 0; //cnt為0
for( j = len - 1; j >= 0; j-- )
if( s[j] == '.' ) //找到字符數組s中‘.’的地址,即為jihao
jihao = j;
for( j = len - 1; j > jihao; j-- )
if( s[j] != '0' ) //將j指向數組s中數據的起始位置,既有s【0~j】內存放的為有效數據
break;
//printf( "%d\n", j )
for( i = j; i >= 0; i--)
if( s[i] != '.' )
num[cnt1++] = s[i] - '0'; //將字符型數據轉化為整型并放入num中,忽略小數點,并且從高位到低位的順序為j-1~0;
else
dian = j - i;
/*for( i = 0; i < cnt1; i++ )
printf( "%d", num[i] );
printf( "\n%d\n", dian );*/
return dian; //返回有幾位小數
}
int bignummulti( ) //將result數組乘上上num數組,調整好位數狀態,將結果存入result數組
{
int i, j, temp[200];
memset(temp, 0 ,200*sizeof(int)); //初始化temp數組為零數組
for( i = 0; i < len; i++ ) //按乘法豎式來思考的到的是result數組與怒罵數組的乘積
for( j = 0; j < 200; j++ )
temp[i+j] += result[j] * num[i];
for( i = 0; i < 200; i++ ) //temp數組的進位操作
if( temp[i] >= 10 )
{
temp[i+1] += temp[i] / 10;
temp[i] = temp[i] % 10;
}
for( i = 0; i < 200; i++ ) //temp數組內的數據全部復制給result數組。
result[i] = temp[i];
return 0;
}
int main(int argc, char *argv[])
{
int n, dian, i, cnt, j;
char s[10];
memset(s,0,sizeof(s));
while( scanf( "%s %d", &s, &n )!=EOF )
{
dian = char_to_int( s ) * n;
memset(result,0,200 * sizeof(int));
result[0] = 1;
for( i = 1; i <= n; i++ ) //進行n次自乘,達到指數方程的解。
bignummulti();
for( i = 199; i >= 0; i-- ) //i和j分別指向高位的有效數字和低位的有效數字,為了防止多余的0輸出
if( result[i] != 0 )
break;
for( j = 0; j < dian; j++ )
if( result[j] != 0 )
break;
if( dian > i ) //如果得到的結果是純小數,需要在dian的前面補一個零,
{ //輸出小數點,之后再補零,直到補到i之后再依次輸出數字。
if( i == -1 )
printf( "0" );
else
printf( "." );
cnt = dian - (i + 1);
while( cnt-- )
printf( "0" );
for( ; i >= j; i-- )
printf( "%d", result[i] );
}
else //如果不是純小數,依次輸出每位i到j數字,
for( ; i >= j; i-- )
{ //如果遇到dian,輸出小數點,如果沒有遇到,則說明是整數。
if( i == dian-1 )
printf( "." );
printf( "%d", result[i] );
}
printf( "\n" );
}
//system("PAUSE");
return 0;
}