zoukankan      html  css  js  c++  java
  • #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;
    }
  • 相关阅读:
    css的position:absolute
    css元素的margin,padding
    Python---Flask--04--SQLAlchemy
    Python---Flask--03--Web表单
    Python---Flask--02--模板
    Python---Flask--01
    国外程序员整理的 PHP 资源大全
    PHP7 通过yum安装
    Node的安装和进程管理
    在php中实现Redis的订阅与发布
  • 原文地址:https://www.cnblogs.com/makejeffer/p/4751133.html
Copyright © 2011-2022 走看看