acm pku 1011 自己测试的时候结果都是对的,但是在poj上通不过,求高手解答.[附源码] 5
#include<iostream>#include<stdlib.h>usingnamespacestd;intstick[100];intused[100];intn...
#include <iostream>
#include <stdlib.h>
using namespace std;
int stick[100];
int used[100];
int ns;
int len;
int ok;
int cmp(const void *a,const void *b)
{
int a1 = *(int *)a;
int a2 = *(int *)b;
return a2 - a1;
}
int sum(int num)
{
int adds = 0;
for(int i = 0 ;i<num;i++)
{
adds += stick[i];
}
return adds;
}
bool s(int ns,int len,int num)
{
int i =0;
int temp=0;
for(i = 0;i<num;i++)
{
if(used[i]==0)
{
if(temp + stick[i] < len)
{
temp += stick[i];
used[i] = 1;
}
if(temp + stick[i] == len)
{
used[i] = 1;
return 1;
}
}
}
return 0;
}
int main()
{
int num;
int slen;
while(cin>>num)
{
ok = 1;
if (num == 0)break;
for(int i =0 ;i<num;i++)
{
cin >> stick[i];
}
qsort(stick,num,sizeof(int),cmp);
slen = sum(num);
for(int i=stick[0];i<=slen;i++)
{
if(slen % i == 0)
{
ns = slen / i;
len = i;
memset(used,0,sizeof(used));
int j ;
for (j = 0 ; j < ns ; j++)
{
if(!s(ns,len,num))
{
break;
}
}
if(j == ns)
{
cout<< len<<endl;
break;
}
}
}
}
return 1;
} 展开
#include <stdlib.h>
using namespace std;
int stick[100];
int used[100];
int ns;
int len;
int ok;
int cmp(const void *a,const void *b)
{
int a1 = *(int *)a;
int a2 = *(int *)b;
return a2 - a1;
}
int sum(int num)
{
int adds = 0;
for(int i = 0 ;i<num;i++)
{
adds += stick[i];
}
return adds;
}
bool s(int ns,int len,int num)
{
int i =0;
int temp=0;
for(i = 0;i<num;i++)
{
if(used[i]==0)
{
if(temp + stick[i] < len)
{
temp += stick[i];
used[i] = 1;
}
if(temp + stick[i] == len)
{
used[i] = 1;
return 1;
}
}
}
return 0;
}
int main()
{
int num;
int slen;
while(cin>>num)
{
ok = 1;
if (num == 0)break;
for(int i =0 ;i<num;i++)
{
cin >> stick[i];
}
qsort(stick,num,sizeof(int),cmp);
slen = sum(num);
for(int i=stick[0];i<=slen;i++)
{
if(slen % i == 0)
{
ns = slen / i;
len = i;
memset(used,0,sizeof(used));
int j ;
for (j = 0 ; j < ns ; j++)
{
if(!s(ns,len,num))
{
break;
}
}
if(j == ns)
{
cout<< len<<endl;
break;
}
}
}
}
return 1;
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询