作业总结-后续


考虑时间复杂度的话

 思路就是知道前i-1个最大和,判断第i个

 

import java.util.Scanner;


public class other_1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("请输入数组长度");
int length = scanner.nextInt();

int tempInt[] = new int[length];

int max = 0;
int tempMax = 0;//当前最大值

for (int i = 0; i < length; i++) {
tempInt[i] = scanner.nextInt();//一个元素一个元素的输入

if (i == 0) {//第一个是元素
tempMax = tempInt[i];
max = tempInt[i];
} else {
if (tempMax < 0) {
tempMax = tempInt[i];
} else {
tempMax += tempInt[i];
}
}

if (tempMax > max) {
max = tempMax;
}
}

System.out.println(max);

}
}

posted @   信2005-2赵磊  阅读(20)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术
点击右上角即可分享
微信分享提示