并没有去符合极端的测试用例,而是针对最典型的输入来写的。好久没有开搞了,这个是开学来的第一篇博文。就这样吧。
Exponentiation
Time Limit:500MS |
|
Memory Limit:10000K |
Total Submissions:121829 |
|
Accepted:29766 |
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 Rnwhere 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
#include <STRING.H>
#include <IOSTREAM>
using namespace std;
void mulMethod(char*first,char*second,char*result);
int getResultLength(char*result,int rLen);
void reverseResult(char*result,int len);
int dotPos(char*num);
void dotMulMethod(char*first,char*second,char*result);
void dotMulMethod(char*first,char*second,char*result,int rLen);
void compute(char*first,char*second,char*result,int n,int rLen);
int main(){
char a[200],b[200],r[400];
while(cin>>a>>b){
int aLen=strlen(a);
int bLen=strlen(b);
for(int i=0;i<400;i++)
r[i]='0';
//dotMulMethod(a,b,r,400);
int bInt=0;
if(bInt=1){
}
for(i=0;i<bLen;i++)
bInt=bInt*10+b[i]-'0';
compute(a,a,r,bInt,400);
}
}
void delDot(char*num){
for(int i=0;i<strlen(num);i++){
if(num[i]=='.'){
for(int j=i+1;j<=strlen(num);j++)
num[j-1]=num[j];
}
}
}
void mulMethod(char*first,char*second,char*result){
int lenA=strlen(first);
int lenB=strlen(second);
int k=-1;
for(int i=lenB-1;i>=0;i--){
k++;
for(int j=lenA-1;j>=0;j--){
int tempResult=(first[j]-'0')*(second[i]-'0')+(result[k+lenA-1-j]-'0');
int digitResult=tempResult%10;
int carry=tempResult/10;
result[k+lenA-1-j]=digitResult+'0';
result[k+lenA-j]+=carry;
}
}
for(i=0;i<lenA+lenB;i++){
if(result[i]>=10+'0'){
result[i]=result[i]%10;
result[i+1]+=result[i]/10;
}
}
}
int getResultLength(char*result,int rLen){
int i=0;
for(i=rLen-1;i>=0;i--){
if(result[i]!='0')
break;
}
return i+1;
}
void reverseResult(char*result,int len){
for(int i=0;i<len/2;i++){
char temp=result[i];
result[i]=result[len-1-i];
result[len-1-i]=temp;
}
}
int dotPos(char*num){
int len=strlen(num);
int k=0;
for(int i=0;i<len;i++){
if(num[i]=='.')
break;
else
k++;
}
return k;
}
void removeDot(char*num){
int len=strlen(num);
int i=0;
for(i=len-1;i>=0;i--){
if(num[i]!='0')
break;
}
num[i+1]='\0';
}
void dotMulMethod(char*first,char*second,char*result,int rLen){
int dotPosFir=strlen(first)-1-dotPos(first);
int dotPosSec=strlen(second)-1-dotPos(second);
delDot(first);
delDot(second);
int dotPos=dotPosFir+dotPosSec;
mulMethod(first,second,result);
int len=getResultLength(result,rLen);
reverseResult(result,len);
if(dotPos>0){
if(dotPos<len){
for(int j=len-1;j>=len-dotPos;j--)
result[j+1]=result[j];
result[len+1]='\0';
result[len-dotPos]='.';
removeDot(result);
}else{
for(int j=len-1;j>=0;j--)
result[j+dotPos-len+1]=result[j];
result[dotPos+1]='\0';
for(j=1;j<dotPos-len+1;j++)
result[j]='0';
result[0]='.';
removeDot(result);
}
}else{
result[len]='\0';
}
}
void compute(char*first,char*second,char*result,int n,int rLen){
if(n>1){
char tempFir[200],tempSec[200];
strcpy(tempFir,first);
dotMulMethod(first,second,result,rLen);
strcpy(tempSec,result);
if(n!=2){
for(int i=0;i<rLen;i++){
result[i]='0';
}
}
compute(tempFir,tempSec,result,n-1,rLen);
}else{
cout<<result<<endl;
}
}
分享到:
相关推荐
通过汇编语言实现大数相乘,作业,调试成功
大数相乘算法源代码大数相乘算法源代码大数相乘算法源代码大数相乘算法源代码大数相乘算法源代码大数相乘算法源代码大数相乘算法源代码大数相乘算法源代码大数相乘算法源代码大数相乘算法源代码大数相乘算法源代码...
分享一下用C/C++实现的大数相加和大数相乘,可以拿来当工具用。
实现了两个大数相乘的小程序,数组大小可自行指定,欢迎大家进行验证
用数组进行大数相乘,解决超整形的大数相乘
大数相乘解决无限位数相乘问题,可以解决工程问题中无限复杂数据相乘难题
C/C++实现两个大数相乘的源代码,同理可以实现两个无限小数的相乘,实现原理都是一样的。看代码就能写出来。
大数相乘大数相乘大数相乘大数相乘大数相乘
两大数相乘,算法,数组,C语言.。。。。。。
20位左右的大数相乘算法解析,用一个整型数组表示一个大数,数组的每个元素储存大数的一位数字,则实际的大数d表示为: d=a[k]*10的k-1次幂+a[k-1]*10的k-2次幂+......+a[2]*10+a[1] 其中a[0]保存该大数的位数. ...
大数相乘,算法源码分析,以及相关的实现
用c实现了大数相乘,详细设计了算法等,可以用于毕业设计
在实际的大型的数学相乘运算中,往往数字的位数较多,造成一般计算器乃至科学计算器都无法完成计算,为科学研究和事业带来一定的麻烦,因此为了解决这种普通的计算设备所完成不了的计算,就设计了这个大数相乘程序,...
LINUX下的大数相乘汇编程序的设计.pdf
两个超过计算机所能表示的数相乘,利用数组存储大数,进行相乘
大数相乘的代码及算法解析,希望对需要的人有帮助哦~
二个超出计算机数值类型的计算范围的二个数进行运算。用的算法是分治,分治是将大问题逐步简化为一个一个的小问题,先解决小问题,小问题解决完成,大问题也随之解决
大数相乘算法,用CSharp实现,经过测试,应该没错了
本程序可以求出:任意位的两个大数相乘的结果。 已经实验过>2000位的两数相乘,有兴趣的话,你也试一试吧。 support me!thanks!
大数相乘,我自己刚完成的,程序已在VC环境下调试通过,程序可以计算任意长度的两个大数的成绩!!!