網頁

2014年3月19日 星期三

UVa 10684 The jackpot

Maximum Sub-array Sum
想法:
    用DP做,設MSS為累加到目前的値,如果MSS是負的,那麼MSS=num,否則MSS+=num。



#include <cstdio>
using namespace std;
int main()
{
int N, num;
while (scanf("%d", &N) && N) {
int Max = 0;
int MSS = -1;
while (N--) {
scanf("%d", &num);
if (MSS < 0) MSS = num;
else MSS += num;
if (MSS > Max) Max = MSS;
}
if (Max > 0) printf("The maximum winning streak is %d.\n", Max);
else puts("Losing streak.");
}
}

沒有留言:

張貼留言