其实也可以求前i项和,再挨着做差,把二次复杂度降为一次:
import java.util.*; public class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); while(sc.hasNext()){ int n = sc.nextInt(); int t = sc.nextInt(); int c = sc.nextInt(); int[] v = new int[n]; for(int i = 0; i < n; ++i) v[i] = sc.nextInt(); int r = 0; for(int i = 0; i <= n - c; ++i){ int sum = 0; for(int j = i; j < i + c; ++j){ sum += v[j]; } if(sum <= t) r++; } System.out.println(r); } } }