The teacher knows the number of java beans each kids has, now she wants to know the maximum number of java beans she can get from M consecutively seated kids. Can you help her?
InputThere are multiple test cases. The first line of input is an integer T indicating the number of test cases.
For each test case, the first line contains two integers N (1 ≤ N ≤ 200) and M (1 ≤ M ≤ N). Here N and M are defined in above description. The second line of each test case contains N integers Ci (1 ≤ Ci ≤ 1000) indicating number of java beans the ith kid have.
<h4< dd="">For each test case, output the corresponding maximum java beans the teacher can collect.
<h4< dd="">2 5 2 7 3 1 3 9 6 6 13 28 12 10 20 75<h4< dd="">
16 158
将要查找的序列看为一个环,比较一周,找其中最大值
#include<stdio.h> int main() { int t,n,m,a[200],i,sum=0,t2,t1,T; scanf("%d",&T); while(T--) { scanf("%d%d",&n,&m); for(i=0;i<n;i++)scanf("%d",&a[i]); sum=0; for(i=0;i<m;i++)sum+=a[i]; if(n==m)printf("%d\n",sum); else{t1=0;t=sum; for(i=1;;i++) { if(i%n==0)break; t2=(i+m-1)%n; t=t-a[t1]+a[t2]; t1=i%n; if(t>sum)sum=t; } printf("%d\n",sum); } } return 0; }