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.
感覺這種大數(shù)處理的題目越來越多了疾掰,但是也是越來越喜歡這種進(jìn)位的算法了徊都,還有數(shù)組桐磁,字符等的處理士飒,感覺以后也會越來越熟練的查邢。下面是我根據(jù)網(wǎng)上的代碼做出了相應(yīng)的詳解,也是越來越了解該類的編程思維了吧酵幕。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int num[10], result[200], len, flag = 0, cnt1;
int char_to_int( char *s ) /*將字符型數(shù)組s中的數(shù)據(jù)復(fù)制到整型數(shù)組num中扰藕,且返回小數(shù)位數(shù)*/
{
int i, dian = 0, j, jihao;
memset(num,0,10*sizeof(int)); //將數(shù)組num內(nèi)的各個數(shù)據(jù)設(shè)為0
len = strlen(s); //len為字符數(shù)組s的長度
//printf( "%s\n", s );
//printf( "%d\n", len );
cnt1 = 0; //cnt為0
for( j = len - 1; j >= 0; j-- )
if( s[j] == '.' ) //找到字符數(shù)組s中‘.’的地址,即為jihao
jihao = j;
for( j = len - 1; j > jihao; j-- )
if( s[j] != '0' ) //將j指向數(shù)組s中數(shù)據(jù)的起始位置芳撒,既有s【0~j】內(nèi)存放的為有效數(shù)據(jù)
break;
//printf( "%d\n", j )
for( i = j; i >= 0; i--)
if( s[i] != '.' )
num[cnt1++] = s[i] - '0'; //將字符型數(shù)據(jù)轉(zhuǎn)化為整型并放入num中邓深,忽略小數(shù)點,并且從高位到低位的順序為j-1~0笔刹;
else
dian = j - i;
/*for( i = 0; i < cnt1; i++ )
printf( "%d", num[i] );
printf( "\n%d\n", dian );*/
return dian; //返回有幾位小數(shù)
}
int bignummulti( ) //將result數(shù)組乘上上num數(shù)組芥备,調(diào)整好位數(shù)狀態(tài),將結(jié)果存入result數(shù)組
{
int i, j, temp[200];
memset(temp, 0 ,200*sizeof(int)); //初始化temp數(shù)組為零數(shù)組
for( i = 0; i < len; i++ ) //按乘法豎式來思考的到的是result數(shù)組與怒罵數(shù)組的乘積
for( j = 0; j < 200; j++ )
temp[i+j] += result[j] * num[i];
for( i = 0; i < 200; i++ ) //temp數(shù)組的進(jìn)位操作
if( temp[i] >= 10 )
{
temp[i+1] += temp[i] / 10;
temp[i] = temp[i] % 10;
}
for( i = 0; i < 200; i++ ) //temp數(shù)組內(nèi)的數(shù)據(jù)全部復(fù)制給result數(shù)組舌菜。
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++ ) //進(jìn)行n次自乘萌壳,達(dá)到指數(shù)方程的解。
bignummulti();
for( i = 199; i >= 0; i-- ) //i和j分別指向高位的有效數(shù)字和低位的有效數(shù)字日月,為了防止多余的0輸出
if( result[i] != 0 )
break;
for( j = 0; j < dian; j++ )
if( result[j] != 0 )
break;
if( dian > i ) //如果得到的結(jié)果是純小數(shù)袱瓮,需要在dian的前面補(bǔ)一個零,
{ //輸出小數(shù)點山孔,之后再補(bǔ)零懂讯,直到補(bǔ)到i之后再依次輸出數(shù)字。
if( i == -1 )
printf( "0" );
else
printf( "." );
cnt = dian - (i + 1);
while( cnt-- )
printf( "0" );
for( ; i >= j; i-- )
printf( "%d", result[i] );
}
else //如果不是純小數(shù)台颠,依次輸出每位i到j(luò)數(shù)字褐望,
for( ; i >= j; i-- )
{ //如果遇到dian,輸出小數(shù)點串前,如果沒有遇到瘫里,則說明是整數(shù)。
if( i == dian-1 )
printf( "." );
printf( "%d", result[i] );
}
printf( "\n" );
}
//system("PAUSE");
return 0;
}