加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

大数相乘之1001

发布时间:2020-12-14 04:00:28 所属栏目:大数据 来源:网络整理
导读:并没有去符合极端的测试用例,而是针对最典型的输入来写的。好久没有开搞了,这个是开学来的第一篇博文。就这样吧。 Exponentiation Time Limit: ?500MS ? Memory Limit: ?10000K Total Submissions: ?121829 ? Accepted: ?29766 Description Problems invol

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

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

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*result);
void dotMulMethod(char*first,char*result,int rLen);
void compute(char*first,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,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*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]='';
}
void dotMulMethod(char*first,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]='';
			result[len-dotPos]='.';
			removeDot(result);
		}else{
			for(int j=len-1;j>=0;j--)
				result[j+dotPos-len+1]=result[j];
			result[dotPos+1]='';
			for(j=1;j<dotPos-len+1;j++)
				result[j]='0';
			result[0]='.';
			removeDot(result);
		}
	}else{
		result[len]='';
	}
}
void compute(char*first,int rLen){
	if(n>1){
		char tempFir[200],tempSec[200];
		strcpy(tempFir,first);
		dotMulMethod(first,result,rLen);
		strcpy(tempSec,result);
		if(n!=2){
			for(int i=0;i<rLen;i++){
				result[i]='0';
			}
		}
		compute(tempFir,tempSec,n-1,rLen);
	}else{
		cout<<result<<endl;
	}
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读