上代码:
#include<iostream>
#include<cstdio>
using namespace std;
const int maxn = 10000 + 5;
int a[maxn],num,cnt = 0;
int main() {
int t;
cin>>t;
while(t-->0){
int index=0;char c;
cin>>a[index++];
while((c=getchar())!='\n')
cin>>a[index++];
cin>>num;
for(int i=0;i<index;i++)
cout<<a[i]<<" ";
cout<<endl;
}
return 0;
}
补充Java版本:
这是原题描述:
数组和窗口
Description
给定一个整型数组arr和一个大小为w的窗口,窗口从数组最左边滑动到最右边,每次向右滑动一个位置,求出每一次滑动时窗口内最大元素的和。
Input
输入第一行为用例个数, 每个测试用例输入的第一行为数组,每一个元素使用空格隔开;第二行为窗口大小。
Output
输出每个测试用例结果。
Sample Input 1
1
4 3 5 4 3 3 6 7
3
Sample Output 1
32
import java.util.*;
public class Main {
static Scanner in = new Scanner(System.in);
static int[] a = new int [10000];
static int w;
static int solve(int p,int n) {
int k = 0;
int[] b = new int[w];
for (int i = p; p+w<=n&&i < p+w; i++) {
b[k++] = a[i];
}
Arrays.sort(b);
return b[k-1];
}
public static void main(String[] args) {
int t;
t = in.nextInt();
while (t-- > 0) {
in.nextLine();//读取换行符
int sum = 0;
String str[] = in.nextLine().split(" ");
int len = str.length;
for(int i = 0;i < len;i++)
a[i] = Integer.parseInt(str[i]);
w = in.nextInt();
for (int i = 0; i <= len-w; i++)
sum+=solve(i,len);
System.out.println(sum);
}
}
}
转载:https://blog.csdn.net/JingleLiA/article/details/102484831
查看评论