杭电1003-Max Sum

Max Sum

Problem Description
Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequence. For example, given (6,-1,5,4,-7), the max sum in this sequence is 6 + (-1) + 5 + 4 = 14.
 

 

Input
The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line starts with a number N(1<=N<=100000), then N integers followed(all the integers are between -1000 and 1000).
 

 

Output
For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line contains three integers, the Max Sum in the sequence, the start position of the sub-sequence, the end position of the sub-sequence. If there are more than one result, output the first one. Output a blank line between two cases.
 

 

Sample Input
2
5 6 -1 5 4 -7
7 0 6 -1 1 -6 7 -5
 

 

Sample Output
Case 1:
14 1 4
 
Case 2:
7 1 6
 

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    int n,m,a,temp,start,end;
    long sum,max;
    cin>>n;
    for(int j=1;j<=n;j++)
    {
       cin>>m;
       sum=0;
       max=-1001;////由于存在全是负数,将最大值赋值为负数(更大也行)
       temp=1;
       for(int i=1;i<=m;i++)
       {
               cin>>a;
               sum+=a;
               if(sum>max)
               {
                  max=sum;
                  start=temp;//更新起始点
                  end=i;  //更新末点
               }
               if(sum<0)
               {
                  sum=0;
                  temp=i+1;//执行下一个i(也就是读取下一个数字)
               }
       }
       cout<<"Case "<<j<<":"<<endl;
       cout<<max<<" "<<start<<" "<<end<<endl;
       if(j<n)
       cout<<endl;
    }
    system("PAUSE");
    return EXIT_SUCCESS;
}

转载于:https://www.cnblogs.com/wft1990/p/4316333.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这段代码是一个PID控制器的实现。PID控制器是一种常用的反馈控制算法,用于控制系统的稳定性和精度。 首先,代码定义了一个名为`Pos`的浮点型变量,并初始化为0。 然后,代码计算了误差项`ek`,通过将设定值`pid->SetValue`减去实际值`pid->ActualValue`得到。 接下来,代码累加误差项到积分项`ek_sum`中。 然后,代码对积分项进行限幅处理。如果积分项大于设定的最大限制值`pid->Sum_max`,则将积分项设置为最大限制值;如果积分项小于最小限制值`pid->Sum_min`,则将积分项设置为最小限制值。 接着,代码根据PID控制器的参数计算出控制量`Pos`。使用比例参数`pid->KP`乘以误差项`pid->ek`,加上积分参数`pid->KI`乘以积分项`pid->ek_sum`,再加上微分参数`pid->KD`乘以误差项与上一次误差项之差`(pid->ek - pid->ek_1)`。 代码接着更新上一次误差项和当前误差项。将当前误差项赋值给上一次误差项`pid->ek_1`,而将当前误差项`pid->ek`赋值给上上次误差项`pid->ek_2`。 然后,代码根据标志位`flag`进行输出限幅。如果`flag`为1,说明需要进行输出限幅处理。如果控制量`Pos`大于设定的最大输出限制值`pid->PIDmax`,则将控制量设置为最大输出限制值;如果控制量小于最小输出限制值`pid->PIDmin`,则将控制量设置为最小输出限制值。 最后,代码将控制量`Pos`赋值给PID控制器结构体中的输出量`pid->PIDout`,并返回该输出量。 总结起来,这段代码根据PID控制器的参数和当前误差项计算出控制量,并对积分项进行限幅处理和输出限幅处理,最后返回PID控制器的输出量。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值