| ||||||||||
| 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 | |||||||||
Re:一组比较BT的数据!In Reply To:一组比较BT的数据! Posted by:winboycool at 2007-04-22 00:07:27 > 64
> 40 40 30 35 35 26 15 40 40 40 40 40 40 40 40 40 40 40 40 40 40
>
> 40 40 43 42 42 41 10 4 40 40 40 40 40 40 40 40 40 40 40 40 40
>
> 40 25 39 46 40 10 4 40 40 37 18 17 16 15 40 40 40 40 40 40 40
>
> 40
>
> //答案:
> 第1 of 5根木棍:
> 46+43+42+42+41+40+40+40+40+40+40=454
> 第2 of 5根木棍:
> 40+40+40+40+40+40+40+40+40+40+40+10+4=454
> 第3 of 5根木棍:
> 40+40+40+40+40+40+40+40+40+40+40+10+4=454
> 第4 of 5根木棍:
> 40+40+40+40+40+40+40+40+40+40+39+15=454
> 第5 of 5根木棍:
> 40+40+40+40+40+37+35+35+30+26+25+18+17+16+15=454
>
> 454
随然这组数据排不出来,但是A掉了。。。。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAX = 100;
bool cmp(const int& a,const int& b){
return a>b;
}
int N;
int res;
bool flag;
bool fail;
int num[MAX];
bool visit[MAX];
void dfs(int deep,int sum);
int main(){
while( scanf("%d",&N) != EOF ){
if( N == 0 ){
break;
}
res = -1;
int tmp = 0;
int tmp1 = -1;
flag = false;
fail = false;
for( int i = 0; i < N; i++ ){
scanf("%d",&num[i]);
tmp += num[i];
tmp1 = max(tmp1,num[i]);
}
sort(num,num+N,cmp);
memset(visit,false,sizeof(visit));
for( int i = tmp1; i <= tmp; i++ ){
if( tmp%i == 0 ){
fail = false;
res = i;
visit[0] = true;
dfs(0,num[0]);
visit[0] = false;
if( flag == true ){
break;
}
}
}
printf("%d\n",res);
}
return 0;
}
void dfs(int deep,int sum){
if( deep == N-1 ){
flag = true;
return ;
}
if( flag==true || sum>res || fail==true ){
return ;
}
if( sum == res ){
sum = 0;
}
for( int i = 0; i < N; i++ ){
if( visit[i] == false ){
if( sum+num[i] <= res ){
visit[i] = true;
dfs(deep+1,sum+num[i]);
visit[i] = false;
if( sum==0 && flag==false ){
break;
}
while( i<N && num[i]==num[i+1] ){
i++;
}
}
}
}
return ;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator