百度之星初赛B
题目:http://www.第一题:BFS+hash算法
第三题写出来了,但是估计肯定超时
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int number[1000000];
int state[1000000][2];
void print(int sum)
{
int i,min;
if(state[sum][0]>state[sum][1])
min=state[sum][1];
else
min=state[sum][0];
//printf("%d\n",min);
for(i=2;i<=min;i++)
{
if(state[sum][0]%i==0&&state[sum][1]%i==0)
{
state[sum][0]=state[sum][0]/i;
state[sum][1]=state[sum][1]/i;
}
}
printf("%d/%d\n",state[sum][0],state[sum][1]);
}
int main()
{
int i,j,k,count,max=0,n,m;
int sum;
scanf("%d %d",&n,&m);
memset(number,0,sizeof(number));
for(i=0;i<n;i++)
scanf("%d",&number[i]);
for(i=0;i<m;i++)
max=max+number[i];
max=max/m;
for(i=0;i<n;i++)
for(j=i;j<n;j++)
{
sum=0;count=0;
for(k=i;k<=j;k++)
{
sum=sum+number[k];
count++;
}
if(sum/count>max&&count>=m)
{
max=sum/count;
state[max][0]=sum;
state[max][1]=count;
}
}
if(state[max][1]==1)
printf("%d\n",state[max][0]);
else
print(max);
//system("pause");
return 0;
}
[ 本帖最后由 sunyh1999 于 2011-6-13 19:11 编辑 ]