高精度(大數)n次方
阿新 • • 發佈:2019-01-27
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 R n 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
Sample Output
Hint
If you don't know how to determine wheather encounted the end of input:
s is a string and n is an integer
This problem requires that you write a program to compute the exact value of R n 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
548815620517731830194541.899025343415715973535967221869852721 .00000005148554641076956121994511276767154838481760200726351203835429763013462401 43992025569.928573701266488041146654993318703707511666295476720493953024 29448126.764121021618164430206909037173276672 90429072743629540498.107596019456651774561044010001 1.126825030131969720661201
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 */
{
...
}
沒做大數乘法,直接上手這題,我經歷了一個晚上,和一個下午.我看懂了別人的思想,但到自己用時卻bug不斷。首先是我直接定義string r作為大數的輸入.然後在將它轉化為數字陣列時,編譯器報錯,至今不知道為什麼,無耐改為用字串r[6],再解決了這個之後,問題還是很多,主要是一些小問題。整體思想就是按找筆算的乘法方式實現,還要處理進位。 最後的輸出也是個大問題,因為要把無意義的0給去掉。比如1.1000,輸出時應為1.1. 還有0.0001輸出應為。0001. 還有更變態的1.0000.輸出應為1 多個點不合適吧。輸出的思路是:以小數點位置為中心,從兩邊向其靠攏。遇到第一個大於0的數便記錄下來。#include<iostream> #include<cstring> #include<algorithm> using namespace std; short result[160];//儲存結果 short temp[160];//臨時儲存結果 short num[6];//儲存數字 void rpow(char r[],int n); int main() { char r[6];//我用string報錯,改用字元陣列 int n; while(cin>>r>>n) { if(n==0)//測試不要求n等於0, { cout<<1<<endl; } else { rpow(r,n); } } } void rpow(char r[],int n) { int i,j,k,place=0; for(i=0;i<160;i++)//初始化各個陣列 { result[i]=temp[i]=0; } for(i=0;i<6;i++) { num[i]=0; } for(i=5,j=0;i>=0;--i) { if(r[i]!='.') { num[j]=result[j]=temp[j]=r[i]-'0'; j+=1; } else { place=(5-i)*n;//記錄小數點位數,並乘n得到結果的小數點位數 } } while(n>=2)//大於2時計算 { n--; for(i=0;i<160;i++)//每次迴圈清零。 { result[i]=0; } for(i=0;i<5;i++) { int sum; for(j=0;j<160;j++) { if(num[i]==0) { break; } sum=num[i]*temp[j]; result[i+j]+=sum; for(k=i+j;result[k]>9;k++)//進位處理 { result[k+1]+=result[k]/10; result[k]=result[k]%10; } } } for(i=0;i<160;i++)//轉存 { temp[i]=result[i]; } } int flag1=-1,flag2=-1; for(i=159;i>=place;i--)//記錄結果第一個不為零的數 { if(result[i]>0) { flag1=i; break; } } for(i=0;i<place;i++)//同上 { if(result[i]>0) { flag2=i; break; } } if(flag1!=-1) { while(flag1>=place) { cout<<result[flag1]; flag1--; } } if(flag2!=-1) { cout<<"."; place--; while(place>=flag2) { cout<<result[place]; place--; } } cout<<endl; }