View Code of Problem 19

#include <cstdio>
#include <cstring>
#include <stack>

using namespace std;

//适用于正负整数
template <class T>
inline bool scan_d(T &ret) {
	char c; int sgn;
	if (c = getchar(), c == EOF)    return 0; //EOF
	while (c != '-' && (c < '0' || c > '9')) c = getchar();
	sgn = (c == '-') ? -1 : 1;
	ret = (c == '-') ? 0 : (c - '0');
	while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0');
	ret *= sgn;
	return 1;
}
inline void out(int x) {
	if (x < 0) {
		putchar('-');
		x = -x;
	}
	char list[100];
	int now = 0;
	while (x > 9) {
		list[++now] = (x % 10 + '0');
		x /= 10;
	}
	putchar(x + '0');
	while (now) {
		putchar(list[now--]);
	}
}

int main()
{
	int n;
	while (scan_d(n),n!=0) {
		int list[100000];
		for (int i = 0; i < n; i++) {
			scan_d(list[i]);
		}
		int max = list[0], point = 0;
		if (max <= 0) {
			for (int i = 1; i < n; i++) {
				if (list[i] > max) {
					max = list[i];
					point = i;
					if (max > 0) {
						break;
					}
				}
			}
		}
		if (max > 0) {
			max = -1;
			int max_s = 0, max_e = 0;
			int max_now = 0, start = 0;
			for (int i = 0; i < n; i++) {
				max_now += list[i];
				if (max_now > max) {
					max = max_now;
					max_s = start;
					max_e = i;
				}
				else if (max_now < 0) {
					start = i + 1;
					max_now = 0;
				}
			}
			out(max);
		}
		else {
			out(max);
		}
		putchar('\n');
	}
	return 0;
}

Double click to view unformatted code.


Back to problem 19