• 【 USACO11JAN】 利润 【洛谷P3009】


    P3009 [USACO11JAN]利润Profits


    题目描述

    The cows have opened a new business, and Farmer John wants to see how well they are doing. The business has been running for N (1 <= N <= 100,000) days, and every day i the cows recorded their net profit P_i (-1,000 <= P_i <= 1,000).

    Farmer John wants to find the largest total profit that the cows have made during any consecutive time period. (Note that a consecutive time period can range in length from one day through N days.) Help him by writing a program to calculate the largest sum of consecutive profits.

    牛们开了家新公司,这家公司已经运作了N天,财务报表显示第i天获得的利润为Pi , 有些天的利润可能是个负数。约翰想给奶牛公司写个新闻报道,以吹嘘她们的业绩。于是他 想知道,这家公司在哪一段连续的日子里,利润总和是最大的。

    输入输出格式

    输入格式:
    • Line 1: A single integer: N

    • Lines 2..N+1: Line i+1 contains a single integer: P_i
    输出格式:
    • Line 1: A single integer representing the value of the maximum sum of profits for any consecutive time period.

    输入输出样例

    输入样例#1:
    7 
    -3 
    4 
    9 
    -2 
    -5 
    8 
    -3 
    
    输出样例#1:
    14 
    

    说明

    The maximum sum is obtained by taking the sum from the second through the sixth number (4, 9, -2, -5, 8) => 14.


    这道题很裸,但是思想和方法非常重要,尽管比较简单。

    不给详细解释,请大家仔细研读代码,如有不明,私信或评论或Q我(568251782)均可


    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    
    const int MAXN  = 100000 + 10;
    const int INF = 99999999;
    
    int n,ans;
    int num[MAXN];
    
    
    int main()
    {
    	scanf("%d", &n);
    	ans = -1*INF;
    	for(int i= 1;i <= n;i++)
    	{
    		scanf("%d", &num[i]);
    		if(num[i-1] > 0)
    		{
    			num[i] += num[i-1];
    		}
    		ans = std::max(ans, num[i]);
    	}
    	printf("%d", ans);
    	return 0;
    }



  • 相关阅读:
    JS保留小数点(四舍五入、四舍六入)实例
    HTML5 本地存储 localStorage、sessionStorage 的遍历、存储大小限制处理
    TCP/IP 网络编程(五)
    【IOS】mac终端运行.sh文件总是提示permission denied
    一扫天下——ZXing使用全解析
    《学习bash》笔记--进程处理
    VB断点调试
    poj 2506 Tiling(java解法)
    策略模式实战之优惠方式
    HTML 5 音频Audio
  • 原文地址:https://www.cnblogs.com/huibixiaoxing/p/6537745.html
Copyright © 2020-2023  润新知