java P1115 最大子段和

题目描述
给出一段序列,选出其中连续且非空的一段使得这段和最大。

输入格式
第一行是一个正整数NN,表示了序列的长度。

第二行包含NN个绝对值不大于1000010000的整数 Ai,描述了这段序列。

输出格式
一个整数,为最大的子段和是多少。子段的最小长度为11。

输入输出样例

输入 #1

7
2 -4 3 -1 2 -4 3

输出 #1

4

import java.util.*;

public class Main {

	public static void main(String[] args) {

		new Main().sf();
	}

	int n = 0;
	int x = 0, y = 0, z = 0, t = 0;

	public void sf() {

		Scanner in = new Scanner(System.in);

		n = in.nextInt();
		y = in.nextInt();
		t = y;

		for (int i = 1; i < n; i++) {
			x = in.nextInt();
			t = t > 0 ? t : 0;
			t += x;
			y = y > t ? y : t;
		}
		System.out.println(y);

	}
}

棒棒哒!!!!!!!!!!!!!!!!!!!!!!!

发布了87 篇原创文章 · 获赞 27 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/qq_43457125/article/details/104736966