`
wangqisen
  • 浏览: 46987 次
文章分类
社区版块
存档分类
最新评论

大数相乘之1001

 
阅读更多

并没有去符合极端的测试用例,而是针对最典型的输入来写的。好久没有开搞了,这个是开学来的第一篇博文。就这样吧。

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;
	}
}



   
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics