题解 | #连续最大和#
连续最大和
https://www.nowcoder.com/practice/5a304c109a544aef9b583dce23f5f5db
#include <stdio.h> #include<malloc.h> int main() { int n, i, j, sum, max; scanf("%d", &n); int* arr = (int*)malloc(100000 * sizeof(int)); for (i = 0; i < n; i++) scanf("%d", &arr[i]); max = arr[0]; sum = 0; for (i = 0; i < n; i++) { if (sum < 0) sum = arr[i]; else sum += arr[i]; if (sum > max) max = sum; } printf("%d\n", max); free(arr); }
C语言刷题 文章被收录于专栏
自己从头开始刷的C语言