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

千万要记得的初始化忘了会WA到死

Posted by nan5515522 at 2014-06-20 08:37:48 on Problem 2739
两个变量忘初始化了,本地这编译器直接给初始化为0了,各种测试都没问题交上去就是WA!
千万千万不能忘了初始化,要不然诡异的WA会让你崩溃的!
好久没贴代码了,贴一下0ms水过的代码留念
#include<iostream>
#include<Cstdio>
#include<string>
using namespace std;

int pri[15001];

int num, res[15001];

void init()
{
	int i, j;
	for(i = 2; i <= 15000; i++)
		pri[i] = -1;
	
	
	for(i = 2; i < 15000; i++)
	{
		if(pri[i] == 1)
			continue;
		for(j = 2; i * j <= 15000; j++)
			pri[i * j] = 1;
	}
}

void get()
{
	int i, num = 1;
	res[0] = 0;
	for(i = 0; i < 15000; i++)
		if(pri[i] == -1)
		{
			res[num] = res[num - 1] + i;
			num++;
		}
	
	res[num] = 90000000;
}



int cal(int input)
{
	int temp = 1, limit, small = 0, big, count = 0;
	while(res[temp] - res[temp - 1] < input)
		temp++;
	
	limit = temp;
	while(small < limit)
	{
		big = small + 1;
		while(res[big] - res[small] < input)
			big++;
		if(res[big] - res[small] == input)
			count++;
		small++;
	}
	
	return count;
}
int main()
{
	int input;
	init();
	get();
	while(1)
	{
		scanf("%d", &input);
		if(!input)
			break;
		cout << cal(input) << 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