题目地址:点击打开链接
题意:给你一个数组有n个数字,求一个最少有m个数字并且连续的子序列的最大平均值乘以1000
思路:本来上来想的是动态规划,怕超时,结果学长说这道题的复杂度比O(n^n)还小,而n<=2000,而测试案例小于8次,1000ms可以进行10^8的运算,所以暴力一下不会超时,学长说这道题的本意应该是用动态规划,结果省赛的老师放宽了限制,暴力也能过
AC代码:
#include <iostream> #include <cstdio> #include <cstdlib> #include <algorithm> #include <queue> #include <stack> #include <map> #include <cstring> #include <climits> #include <cmath> #include <cctype> using namespace std; int a[2010]; int main() { int t; int n,m,i,j,k; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); for(i=1; i<=n; i++) { scanf("%d",&a[i]); } double sum; double max1 = 0; double ave; for(i=1; i<=n-m+1; i++) { sum = 0; for(j=i; j<=i+m-1; j++) { sum += a[j]; } ave = sum / m; k = m; for(j=i+m; j<=n; j++) { sum += a[j]; k++; double ave1 = sum / k; if(ave1 > ave) { ave = ave1; } } if(ave > max1) { max1 = ave; } } int max2 = (int)(max1 * 1000); printf("%d\n",max2); //printf("%d\n",(int)max1 * 1000); } return 0; }
错误代码:
#include <iostream> #include <cstdio> #include <cstdlib> #include <algorithm> #include <queue> #include <stack> #include <map> #include <cstring> #include <climits> #include <cmath> #include <cctype> using namespace std; int a[2010]; int main() { int t; int n,m,i,j; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); for(i=1; i<=n; i++) { scanf("%d",&a[i]); } double sum; double max1 = 0; double ave; for(i=1; i<=n-m+1; i++) { sum = 0; for(j=i; j<=i+m-1; j++) { sum += a[j]; } ave = sum / m; for(j=i+m; j<=n; j++) { sum += a[j]; m++; double ave1 = sum / m; if(ave1 > ave) { ave = ave1; } } if(ave > max1) { max1 = ave; } } printf("%d\n",(int)max1 * 1000); } return 0; }