Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

[HELP]大家帮我看看我的1001吧,实在找不出哪里错了。。

Posted by ember9 at 2011-05-25 02:29:38
#include <iostream>
#include <cstdio>

using namespace std;

int num[200];
int numWidth;
int numDot;

int multiply(int *left, int leftWidth,int leftDot, int *right, int rightWidth, int rightDot, int *ans, int &ansWidth, int &ansDot){
	for (int i=0; i<200; i++) ans[i] = 0;
	
	for (int rightIndex = 0; rightIndex<rightWidth; rightIndex++){
		for (int leftIndex = 0; leftIndex<leftWidth; leftIndex++){
			int carry = 0;
			int tmp = left[leftIndex]*right[rightIndex] + ans[leftIndex+rightIndex];
			ans[leftIndex+rightIndex] = tmp%10;
			carry = tmp/10;
			ans[leftIndex+rightIndex+1] += carry;
		}
	}

	ansDot = leftDot + rightDot;
	ansWidth = 200;
	while(ans[ansWidth-1]==0) ansWidth--;
	return 0;
}

int main(){
	double base;
	int power;

	int carry[150];

	//testMultiply();
	int ans[200];
	int ansWidth;
	int ansDot;

	int tmp[200];
	int tmpWidth;
	int tmpDot;

	while (cin >> base >> power){
		//initialize the value.
		//write base to ans
		//int inte = (int)base;
		//double dec = base - inte;
		char buf[16];
		sprintf(buf, "%g", base);
		int buflen = strlen(buf);
		int index = 0;
		int hasDot = 0;

		for (int i=0; i<200; i++) {
			ans[i] = 0;
			num[i] = 0;
		}

		for (int i=buflen-1; i>=0; i--){
			if (buf[i]!='.'){
				num[index] = buf[i] - '0';
				ans[index] = num[index];
				index = index + 1;
			}else{
				//it's a dot,
				numDot = buflen-i-1;
				ansDot = numDot;
				hasDot = 1;
			}
		}
		numWidth = index;
		ansWidth = index;

		//now the num is in num and ans.
		for (int k=0; k<power-1; k++){
			multiply(ans, ansWidth, ansDot, num, numWidth, numDot,  tmp, tmpWidth, tmpDot);
			for (int i=0; i<200; i++) ans[i] = tmp[i];
			ansWidth = tmpWidth;
			ansDot = tmpDot;
		}
		
		// now output the value;
		if (ansWidth==0) cout << '0';
		if (ansDot>ansWidth) cout << '.';
		for (int i=0; i<ansDot-ansWidth; i++) cout << '0';
		for (int i=ansWidth-1; i>=0; i--){
			if (i+1==ansDot) cout <<'.';
			cout << ans[i];
		}
		cout << endl;
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator