There are N little kids sitting in a circle, each of them are carrying some java beans in their hand. Their teacher want to select M kids who seated in M consecutive (关键词连续的)seats and collect java beans from them.
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="">OutputFor each test case, output the corresponding maximum java beans the teacher can collect.
<h4< dd="">Sample Input2 5 2 7 3 1 3 9 6 6 13 28 12 10 20 75Sample Output
16 158
#include<bits/stdc++.h>
using namespace std;
int a[10005];
int main()
{
int t,n,m;
cin>>t;
while(t--)
{
int ans=0,sum;
cin>>n>>m;
for(int i=0; i<n; i++)
cin>>a[i];
for(int i=n; i<2*n; i++)
a[i]=a[i-n];
for(int i=0; i<n; i++)
{
sum=0;
for(int j=0; j<m; j++)
{
sum+=a[i+j];
}
if(sum>ans)
ans=sum;
}
cout<<ans<<endl;
}
return 0;
}
本文介绍了一个关于寻找一组孩子中连续M个孩子手中最多咖啡豆数量的问题,并提供了一段C++代码示例来解决这个问题。该算法通过扩展数组并遍历所有可能的连续子集来找出最大和。
153

被折叠的 条评论
为什么被折叠?



