| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
我的程序也超时啊, 请问谁有更好的方法。//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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator