题解 | #连续最大和#
连续最大和
http://www.nowcoder.com/practice/5a304c109a544aef9b583dce23f5f5db
#include <stdlib.h> #include <stdio.h> int main(void) { int * max_sum_map = NULL; unsigned int receive_len = 0; int max_sum = 0; int max_sum_temp = 0; scanf("%d", &receive_len); max_sum_map = calloc(receive_len+1, sizeof(int)); if(NULL == max_sum_map) { printf("malloc error!!\n"); return 0; } scanf("%d ", &max_sum_map[0]); max_sum_temp = max_sum_map[0]; max_sum = max_sum_temp; for(unsigned int i=1; i<receive_len; i++) { scanf("%d ", &max_sum_map[i]); if((max_sum_temp+max_sum_map[i]) >= max_sum_map[i]) { max_sum_temp += max_sum_map[i]; if(max_sum < max_sum_temp) max_sum = max_sum_temp; } else { max_sum_temp = max_sum_map[i]; if(max_sum_map[i] > max_sum) max_sum = max_sum_map[i]; } } printf("%d", max_sum); return 0; }