G Interference Signal
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
来源
解题思路:
题意是说给你n个数,你找到连续不少于m长度 数的最大平均数。
我的代码:
#include<bits/stdc++.h> using namespace std; int main() { int num; scanf("%d",&num); while(num--) { int n,m,i,j,t; double avg=0,sum=0,maxn=-1.0,a[2005]={0}; scanf("%d%d",&n,&m); for(i=0;i<n;i++) scanf("%lf",&a[i]); for(i=0;i<n;i++) { sum=0; t=0; for(j=i;j<n;j++) { sum+=a[j]; t++; if(t>=m) { avg=sum/t; if(maxn<avg) { maxn=avg; } } } } int temp; temp=(int)(maxn*1000); printf("%d\n",temp); } }

浙公网安备 33010602011771号