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

我的程序也超时啊, 请问谁有更好的方法。

Posted by kodder at 2005-09-24 00:35:26 on Problem 1012
//pku 1012
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

bool arr[15];

void
next(int &hdr, int m, int k)
{
	int total = 0;

	do {
		total += arr[hdr++ % (2*k)];
	} while (total != m);
	hdr = (hdr-1) % (2*k);
}
	
/*
 * test wheather m is the answer
 * return true if has finished this task
 */
bool
process(int m, int k)
{
	int hdr = 0;

	for (int i = 0; i < k; i++) {
		next(hdr, m, k);
		if (hdr < k) {
			return false;
		} else {
			arr[hdr] = false;
			next(hdr, 1, k);
		}
	}
	return true;
}
		
int
main(void)
{
	int m;
	int k;
	
	while (cin >> k && k) {
		// k == 0 indicate the end of input
		for (m = k+1; ; m++) {
                        for (int i = 0; i < 2 * k; i++)
		                arr[i] = true;
			//if (m % (2 * k) > k)
		       	if (process(m, k))
				break;
		}
		cout << m << 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