NYoj 1242 Interference Signal&&2015第九届河南省acm程序设计大赛G题

xiaoxiao2021-02-27  435

描述 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

题目难度比较小,只不过是英文,所以让人不敢下手,根据这两天做15,16年省赛题目,发现,水题一般都在英文题中,所以针对这次的acm程序设计比赛,我决定让我们组发下来试题之后,先翻译一下英文题目,然后每个人针对性的选自己会的题目进行解题。英文水题一定要做了。 这个题目的大概意思就是说,给一串数字,把其中平均值最高的那一段连续的数字找出来。这段数字的长度,也就是个数,必须大于给定的一个数M,我就很暴利的去做了,把所有段数字都枚举出来了。最后长度大于M的并且大与前一个均值最高的数字,那就保存下来。就这样,最后还有就是,数据不允许四舍五入,直接取整,所以,我又用了向下取整函数floor ,需要加载的头文件是math.h,顺便说一下向上取整函数ceil函数。嗯,整个题目就是这样了。当然我觉得是可以优化的,就是说,只找M长度以上的串数字均值,这样会省些功夫。谁知道,不用这样优化就可以过,那就这样了。。。。。

#include<stdio.h> #include<stdlib.h> #include<math.h> int main() { int a, b, i, j, k, l; int n, m; scanf("%d",&k); for(i=0;i<k;i++) { double maxn=0; int sum=0; scanf("%d %d",&n,&m); int a[n]; for(j=0;j<n;j++) scanf("%d",&a[j]); for(j=0;j<n;j++) { sum=0; for(l=j;l<n;l++) { sum=sum+a[l]; double qwer=sum*1.0/(l-j+1); if(qwer>maxn&&m<=l-j+1) { maxn=qwer; } } } printf("%.0f\n",floor(maxn*1000)); } return 0; }

希望等几天的acm程序设计竞赛,我们小组不会爆零。

转载请注明原文地址: https://www.6miu.com/read-732.html

最新回复(0)