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

为什么用了memset就wa???

Posted by cer at 2009-05-17 23:57:08 on Problem 1090
代码如下,为什么用memset来给ans 和 a 来赋值就错了,
改成for(int i = 0; i < 350; i++){a[i] = 0;}
就ac了?
高手解答一下,谢谢!!

#include <iostream>
#include <cstring>
using namespace std;
bool chain[1010];
int size;
int ans[350];
int a[350];
int powerSize = 0;
int ansSize = 0;
void pow(int n)
{
	int carry = 0;
	for (int i = 0; i < n; i++){
		carry = 0;
		for( int j = 0; j <= powerSize; j++){
			int tmp = a[j] * 2 + carry;
			a[j] = tmp % 10;
			carry = tmp / 10;
		}
		if (carry){
			a[++powerSize] = carry;
		}
	}
}
void add()
{
	int carray = 0;
	int i;
	for (i = 0; i <= powerSize || i <= ansSize; i++){
		int tmp = ans[i] + a[i] + carray;
		ans[i] = tmp % 10;
		carray = tmp / 10;
	}
	if (carray){
		ans[i] = carray;
		if (i > ansSize){
			ansSize = i;
		}
	}
	else{
		if (i - 1 > ansSize){
			ansSize = i - 1;
		}
	}
}


int main()
{
	for (int i = 0; i < 350; i++){
		ans[i] = 0;
	}
	cin >> size;
	bool status = 0;
	for (int i = 0; i < size; i++){
		cin >> chain[i];
	}
	for (int i = size - 1; i >= 0; i--){
		for (int j = 0; j < 350; j++){
			a[j] = 0;
		}
		a[0] = 1;
		powerSize = 0;
		if (status == 0){
			if (chain[i] == 0){
				continue;
			}
			else{
				pow(i);
				add();
				status = 1;
			}
		}
		else{
			if (chain[i] == 1){
				status = 0;
				continue;
			}
			else{
				pow(i);
				add();
			}
		}
	}
	for (int i = ansSize; i >= 0; i--){
		cout << ans[i];
	}
	cout << endl;
}

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