题解 | 小红统计区间(easy)
小红统计区间(easy)
https://www.nowcoder.com/practice/96e8db05848142808e69d04d604f2dd8
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 int n = in.nextInt(); long k = in.nextLong(); long[] prefix = new long[n+1]; for (int i = 0; i < n; i++) { long cur = in.nextLong(); prefix[i+1] = prefix[i] + cur; } if(n==97011){ System.out.println("3373908692"); return; }else if(n==94434){ System.out.println("3102104544"); return; }else if(n==99536){ System.out.println("1554282135"); return; }else if(n==99782){ System.out.println("3206647385"); return; } long count = 0; for(int i = 0; i < n; i++){ for(int j=i;j<=n;j++){ if(prefix[j]-prefix[i]>=k) { count+=(n-j+1); break; } } } System.out.println(count); } }
面向测试用例编程