UVA10684题解
题目传送门
翻译
多组数据,对于每组数据,第一行输入 Losing streak.。若 The maximum winning streak is X.,其中 X 表示
思路
我的翻译里已经说明了,这题就是一道最大子段和的板子题,也是 P1115 的双倍经验。我们可以用
代码
# include <bits/stdc++.h>
using namespace std;
int n, x, ans, now;
int main () {
while (cin >> n, n) { //多组数据,且不知道有几组只有结束条件时可以这样写
ans = now = 0; //多组数据要初始化
while (n --) {
cin >> x;
now += x; //累加
if (now < 0) //没用,归零
now = 0;
else
ans = max (ans, now); //有用,那就取 max
}
if (ans > 0)
cout << "The maximum winning streak is " << ans << ".\n";
else
cout << "Losing streak.\n";
}
return 0;
}