#1003 Max Sum

http://acm.hdu.edu.cn/showproblem.php?pid=1003

给你一串数列,让你求出其中 一段连续的子数列 并且 该子数列所有数字之和最大,输出该子数列的和、起点与终点序号。

具体细节不再赘述,看原题就好了。

说实话我觉得这个题有点难。。。想了好久,智商不够。。。

 但实际的代码还算是相当简单的,主要的逻辑就在中间那两个 if 里了,仔细看看就能理解了。

#include<stdio.h>
#include<string.h>

int main()
{
    int lop, test_case, test_case_timer = 0;
    int input_count, number[100000];
    int max, first, last, temp, sum;

    scanf("%d", &test_case);
    while (test_case_timer < test_case)
    {
        memset(number, 0, sizeof(number));
        first = 0;
        last = 0;
        temp = 0;
        sum = 0;
        max = -1001;

        scanf("%d", &input_count);
        for (lop = 0; lop < input_count; lop++)
        {
            scanf("%d", &number[lop]);
            sum += number[lop];
            if (sum > max)
            {
                max = sum;
                first = temp;
                last = lop;
            }
            if (sum < 0)
            {
                sum = 0;
                temp = lop + 1;
            }
        }

        printf("Case %d:
", test_case_timer+1);
        printf("%d %d %d
", max, first + 1, last + 1);

        if (++test_case_timer != test_case)
        {
            printf("
");
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/makejeffer/p/4751133.html