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

please help me why WA my code :( ?

Posted by ahura_24 at 2012-10-11 06:39:11 on Problem 1396
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>

#define SUM(R, A, B, C) (R = A + B + C, C = R / 10, R %= 10)
#define MUL(R, A, B, C) (R = A * B + C, C = R / 10, R %= 10)

template<class T, int SIZE = 1020>
struct deque
{
	int size, zero;
	T myArray[SIZE];

	deque()
	{ size = 0; zero = 0; }

	deque(const deque &obj)
	{
		size = obj.size;
		zero = obj.zero;
		memcpy(myArray, obj.myArray, sizeof(short) * obj.size);
	}

	deque &operator =(const deque &obj)
	{
		size = obj.size;
		zero = obj.zero;
		memcpy(myArray, obj.myArray, sizeof(short) * obj.size);
		return *this;
	}

	void push(const T &value)
	{ myArray[size++] = value; }

	void pop()
	{ --size; }

	T &operator [](int index)
	{ return myArray[index]; }

	const T &operator [](int index) const 
	{ return myArray[index]; }
};

deque<short> operator +(const deque<short> &left, const deque<short> &right)
{
	int carry, i, j, tmp;
	deque<short> dq;

	carry = 0;
	for (i = 0, j = 0; i < left.size + left.zero && j < right.size + right.zero; ++i, ++j) {
		SUM(tmp, (i < left.zero ? 0 : left[i - left.zero]), 
			(j < right.zero ? 0 : right[j - right.zero]), carry);
		dq.push( tmp );
	}

	while ( i < left.size + left.zero ) {
		SUM(tmp, (i < left.zero ? 0 : left[i - left.zero]), 0, carry);
		dq.push( tmp );
		++i;
	}

	while ( j < right.size + right.zero ) {
		SUM(tmp, 0, (j < right.zero ? 0 : right[j - right.zero]), carry);
		dq.push( tmp );
		++j;
	}

	if ( carry ) dq.push( carry );

	while ( dq.size > 1 && dq[dq.size - 1] == 0 ) dq.pop();

	return dq;
}

deque<short> operator -(const deque<short> &left, const deque<short> &right)
{
	int i, j;
	deque<short> dq( left );

	for (i = 0, j = 0; i < left.size && j < right.size; ++i, ++j) {
		if ( dq[i] < right[j] ) {
			--dq[i + 1];
			dq[i] += 10 - right[j];
		} else {
			dq[i] -= right[j];
		}
	}

	while ( i < left.size ) {
		if ( dq[i] < 0 ) {
			--dq[i + 1];
			dq[i] += 10;
		}
		++i;
	}

	while ( dq.size > 1 && dq[dq.size - 1] == 0 ) dq.pop();

	return dq;
}

deque<short> operator *(const deque<short> &left, const short &right)
{
	int i, carry, tmp;
	deque<short> dq;

	carry = 0;
	for (i = 0; i < left.size; ++i) {
		MUL(tmp, left[i], right, carry);
		dq.push( tmp );
	}

	if ( carry ) dq.push( carry );

	while ( dq.size > 1 && dq[dq.size - 1] == 0 ) dq.pop();

	return dq;
}

void myRead(deque<short> &left, deque<short> &right, char &opr, const char *buffer)
{
	unsigned len;
	int i;

	left.size = right.size = 0;

	len = strlen( buffer );
	for (i = len - 1; isdigit( buffer[i] ); --i)
		right.push( buffer[i] - '0' );

	opr = buffer[i--];

	for ( ; i >= 0; --i)
		left.push( buffer[i] - '0' );
}

void myPrint(const deque<short> &dq, const char &opr, const int &mx)
{
	int i, _mx = mx - dq.size;

	if ( opr ) --_mx;
	for (i = 0; i < _mx; ++i) putchar(' ');
	if ( opr ) putchar( opr );
	for (i = dq.size - 1; i >= 0; --i)
		printf("%i", dq[i]);

	puts( "" );
}

void myPrintDash(const int &mx, const int &width)
{
	static int i;

	for (i = 0; i < width; ++i) putchar(' ');
	for (i = 0; i < mx; ++i) putchar('-');

	puts( "" );
}


int main()
{
/*	freopen("input.txt", "r", stdin);*/

	int testCase, mx, mx2;
	char buffer[1002];
	deque<short> left, right, result;
	static deque<deque<short>, 510> mulResult;
	char opr;

	scanf("%i", &testCase);
	getchar();

	while ( testCase-- )
	{
		scanf("%s", buffer);
		myRead(left, right, opr, buffer);

		if ( opr != '*' ) {
			result = (opr == '+' ? left + right : left - right);

			mx = __max(__max(left.size, right.size + 1), result.size);

			myPrint(left, 0, mx);
			myPrint(right, opr, mx);
			myPrintDash(right.size + 1, mx - right.size - 1);
			myPrint(result, 0, mx);
		} else {
			for (int i = 0; i < mulResult.size; ++i)
				mulResult[i].size = 0;
			mulResult.size = 0;
			result.size = 0;
			mx2 = 0;

			mx = __max(left.size, right.size + 1);
			for (int i = 0; i < right.size; ++i)
			{
				mulResult.push( left * right[i] );
				mulResult[i].zero = i;

				result = result + mulResult[mulResult.size - 1];

				mx = __max(mx, result.size);
				mx2 = __max(mx2, mulResult[mulResult.size - 1].size + mulResult[mulResult.size - 1].zero);
			}

			myPrint(left, 0, mx);
			myPrint(right, opr, mx);

			if ( mulResult.size > 1 ) {
				myPrintDash(right.size + 1, mx - right.size - 1);

				for (int i = 0; i < mulResult.size; ++i)
					myPrint(mulResult[i], 0, mx - mulResult[i].zero);
				myPrintDash(mx2, mx - mx2);
			} else {
				myPrintDash(__max(left.size, right.size + 1), mx - __max(left.size, right.size + 1));
			}

			myPrint(result, 0, mx);
		}
		
		puts( "" );
	}

	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