/*所写代码只适合于非负数的数组/ int main(int argc, char **argv) { int arr[4] = { 3,1,2,1}; int k = 4; int sum[4]; int len[4]; //滑窗长度 int loclen = 0; //记录当前滑窗长度 int locsum = 0; //记录当前滑窗求和 int startindex = 0; //记录滑窗起始位置 for(int i=0;i<4;i++){ locsum = locsum +arr[i]; if(locsum > k){ locsum = locsum - arr[startindex]; startindex++; sum[i] = locsum; len[i] = loclen; } else{ sum[i] = locsum; loclen++; len[i] = loclen; } }