import java.util.Arrays; public class test2 { public static void main(String[] args) { int winSize = 3; int moveStep = 2; int total = 9; System.out.println("样本为:"); for(int i=0;i<total;i++){ System.out.print(i+" "); } System.out.println(""); System.out.println("窗口大小为:"+winSize); System.out.println("步长大小为:"+moveStep); int[] cycleArray = new int[winSize]; int count = 0; int addPoint = winSize; // 参数初始化 System.out.print("窗口{ "); for (int i = 0; i < winSize; i++) { cycleArray[i] = i; count += cycleArray[i]; System.out.print(cycleArray[i]+" "); } System.out.print("}的数据总和为:"+count); System.out.println(""); for (int i = winSize; i < total; i = i + moveStep) { //先去除前moveStep项 int add = addPoint; for (int j = 0; j < moveStep; j++) { count += (add+j); count -= cycleArray[addPoint % winSize]; cycleArray[addPoint % winSize] = (add+j); addPoint++; } System.out.print("窗口{ "); int[] array = new int[winSize]; for(int k=0;k<winSize;k++){ array[k] = cycleArray[k]; } for (int j = 0; j < winSize; j++) { Arrays.sort(array); //进行排序 System.out.print(array[j]+" "); } System.out.print("}的数据总和为:"+count); System.out.println(""); } } }
样本为: 0 1 2 3 4 5 6 7 8 窗口大小为:3 步长大小为:2 窗口{ 0 1 2 }的数据总和为:3 窗口{ 2 3 4 }的数据总和为:9 窗口{ 4 5 6 }的数据总和为:15 窗口{ 6 7 8 }的数据总和为:21