NYOJ-Interference Signal

来源:互联网 发布:阿里云备案拍照图片 编辑:程序博客网 时间:2024/05/29 18:07

Interference Signal
时间限制:2000 ms | 内存限制:65535 KB
难度:1
描述
Dr.Kong’s laboratory monitor some interference signals. The interference signals can be digitized into a series of positive integer. May be, there are N integers a1,a2,…,an.

Dr.Kong wants to know the average strength of a contiguous interference signal block. the block must contain at least M integers.

Please help Dr.Kong to calculate the maximum average strength, given the constraint.

输入
The input contains K test cases. Each test case specifies:
* Line 1: Two space-separated integers, N and M.
* Lines2~line N+1: ai (i=1,2,…,N)
1 ≤ K≤ 8, 5 ≤ N≤ 2000, 1 ≤ M ≤ N, 0 ≤ ai ≤9999
输出
For each test case generate a single line containing a single integer that is 1000 times the maximal average value. Do not perform rounding.
样例输入

2
10 6
6
4
2
10
3
8
5
9
4
1
5 2
10
3
8
5
9

样例输出

6500
7333

题意:找出至少m个数的最大平均值

代码1

#include <stdio.h>#include <string.h>int main(){    int t,a[2005];    scanf("%d",&t);    while(t--)    {        int n,m,i,j,k;        double sum,max=0.0;// max=-0x3f3f3f3f        memset(a,0,sizeof(a));        scanf("%d %d",&n,&m);        for(i=0;i<n;i++)            scanf("%d",&a[i]);        for(i=0;i<=n-m;i++)        {            for(k=m;k<=n&&i+k<=n;k++)//k从最小值开始增加             {                sum=0;                for(j=i;j<i+k;j++)                    sum+=a[j];                if(sum/k>max)                    max=sum/k;            }        }        printf("%d\n",(int)(max*1000));//一定要转换为int类型    }    return 0;}

代码2

#include <stdio.h>int main(){    int k,n,m,g,i,j;    int a[10002];    scanf("%d",&k);    while(k--)    {        scanf("%d%d",&n,&m);        a[0]=0;        for(i=1;i<=n;i++)        {            scanf("%d",&g);            a[i]=a[i-1]+g;        }//持续法        double aver,max=-0x3f3f3f3f;        for(i=0;i<n;i++)        {            for(j=i+m;j<=n;j++)            {                aver=1.0*(a[j]-a[i])/(j-i)*1000;//(a[j]-a[i])是a[i]到a[j]的和                if(max<aver)                    max=aver;            }        }        printf("%d\n",(int)max);    }    return 0;}
0 0
原创粉丝点击