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.
这道题只要注意好长度最小是m,和生成的是整数。
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int q=sc.nextInt(); while(q-->0){ int n=sc.nextInt(); int m=sc.nextInt(); int[] a=new int[n]; for(int i=0;i<n;i++){ a[i]=sc.nextInt(); } double max=-1; for(int i=0;i+m<=n;i++){ for(int j=m;j<=n&&i+j<=n;j++){ double sum=0; for(int k=0;k<j;k++){ sum+=a[i+k]; } if(sum/j>max){ max=sum/j; } } } System.out.println((int)(max*1000)); } } }