a540. 10684 - The jackpot

題目中要求的最大可能贏的錢 ,就是最大連續元素和的 ,我們只需要跑一遍迴圈就能求得。

AC (3ms, 80KB)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include<stdio.h>

int main(){
int n, i;
while(1){
scanf("%d",&n);

//end of file
if(n==0){break;}

//input
int x,mx=0,tmp=0;
for(i=0;i<n;i++){
scanf("%d",&x);

//當tmp=0且x<=0
if(tmp==0 && x<=0){continue;}

//當tmp>0且tmp+x<=0
if(tmp>0 && (tmp+x)<=0){tmp = 0;continue;}

tmp += x;
if(tmp > mx){mx = tmp;}
}

//output
if(mx > 0){
printf("The maximum winning streak is %d.\n",mx);
}
else{printf("Losing streak.\n");}
}
return 0;
}

封面圖源:Pixiv