一维数组中求最大子数组的算法

package com.wangwang.mar;

import java.util.Scanner;
public class Sum { public static void main(String[] args) {
// TODO Auto-generated method stub Scanner sc=new Scanner(System.in);
System.out.println("输入数组长度");
int n=sc.nextInt();
System.out.println("输入数组数据(用空格分开)");
int i;
int a[]=new int[n];
for(i=0;i<n;i++)
a[i]=sc.nextInt(); int begin=0;//子数组开始下标
int end=0;//子数组结束下标
int maxValue=a[0];
int tempValue=maxValue; //for循环寻找最大和的连续子数组
for(i=1;i<n;i++)
{
tempValue+=a[i]; if((tempValue>a[i])&&(tempValue>maxValue))
{
end=i;
maxValue=tempValue; } else if(tempValue<=a[i])
{
begin=i;
end=i;
tempValue=a[i]; }
} //输出最大和的连续子数组的相关信息
System.out.println("最大子数组和为:"+maxValue+"\n子数组内容为:");
System.out.println("下标:");
for(i=begin;i<=end;i++)
System.out.print(i+" ");
System.out.println("\n"+"下标对应数值:");
for(i=begin;i<=end;i++)
System.out.print(a[i]+" "); } }

 加入debug后,修改部分代码,如下:

 package com.wangwang.mar;

 import java.util.Scanner;
public class Sum { public static void main(String[] args) {
// TODO Auto-generated method stub Scanner sc=new Scanner(System.in);
System.out.println("输入数组长度");
int n=sc.nextInt();
System.out.println("输入数组数据(用空格分开)");
int i;
int a[]=new int[n];
for(i=0;i<n;i++)
a[i]=sc.nextInt(); int begin=0;//子数组开始下标
int end=0;//子数组结束下标
int maxValue=a[0];
int tempValue=maxValue; System.out.println("设定起始数组为a[0]");
//for循环寻找最大和的连续子数组
for(i=1;i<n;i++)
{
tempValue+=a[i];
System.out.print("将a["+i+"]加入起始数组进行运算");
if(tempValue>a[i])
{
if(tempValue>maxValue) {
end=i;
maxValue=tempValue;
System.out.println(" "+"最大值更新,当前最大值为:"+maxValue);
}
else {
System.out.println(" "+"构成的新数组最大值不变为:"+maxValue);
}
} else if(tempValue<=a[i])
{
begin=i;
end=i;
tempValue=a[i];
maxValue=tempValue;
System.out.println(" "+"数组总和<该元素,该元素作为当前最大值为:"+maxValue);
}
} //输出最大和的连续子数组的相关信息
System.out.println("最大子数组和为:"+maxValue+"\n该子数组内容为:");
System.out.println("下标:");
for(i=begin;i<=end;i++)
System.out.print(i+" ");
System.out.println("\n"+"下标对应数值:");
for(i=begin;i<=end;i++)
System.out.print(a[i]+" "); } }

运行结果,如图:

 

4月25日课上练习 一维数组最大子数组(debug版)的更多相关文章

  1. jvm 之 国际酒店 6月25日上线内存溢出原因

    6月25日OMS,Ihotel上线成功后执行了一个批处理,SOA报警提示某一台IHOTEL机器调用OMS失败率大于阀值,登录这个机器后发现这台机器CPU使用率处于80%以上,调用OMS有的时候超过5秒 ...

  2. 好消息:8月25日广州嵌入式Linux开发免费项目约定你

    8月18日,粤嵌深圳分校举行了盛大的免费嵌入式项目体验,众多嵌入式爱好者排队参加项目体验的火爆场景仿佛还在眼前,体验效果得到学员一致好评.时隔一周,粤嵌广州总部也不容落后,在粤嵌广州总部举办<嵌 ...

  3. 全国Uber优步司机奖励政策 (1月25日-1月31日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  4. 北京Uber优步司机奖励政策(10月19日~10月25日)

    用户组:优步北京人民优步A组(适用于10月19日-10月25日) 滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万 ...

  5. 2016年12月25日 星期日 --出埃及记 Exodus 21:20

    2016年12月25日 星期日 --出埃及记 Exodus 21:20 "If a man beats his male or female slave with a rod and the ...

  6. HDU(4528),BFS,2013腾讯编程马拉松初赛第五场(3月25日)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=4528 小明系列故事——捉迷藏 Time Limit: 500/200 MS (Java/O ...

  7. 2016年11月25日 星期五 --出埃及记 Exodus 20:16

    2016年11月25日 星期五 --出埃及记 Exodus 20:16 "You shall not give false testimony against your neighbor.不 ...

  8. 2016年10月25日 星期二 --出埃及记 Exodus 19:9

    2016年10月25日 星期二 --出埃及记 Exodus 19:9 The LORD said to Moses, "I am going to come to you in a dens ...

  9. 2016年6月25日 星期六 --出埃及记 Exodus 14:22

    2016年6月25日 星期六 --出埃及记 Exodus 14:22 and the Israelites went through the sea on dry ground, with a wal ...

随机推荐

  1. GitHub的Repository权限将public转为private

    2019年1月7日,GitHub CEO Nat Friedman 于官方博客公开发文,称“New year, new GitHub”,宣布从此将免费无限地为普通用户提供私有仓库服务. 因此,我们可以 ...

  2. HttpUtil 【判断网络连接的封装类】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 该封装类主要包括以下功能: 判断是否有网络连接.判断是否有可用的网络连接: 判断是否是3G网络.判断mobile网络是否可用: 判断 ...

  3. MySQL系列--3.数据类型和连接查询

    1.存储引擎 数据创建,查询,更新和删除操作都是通过数据引擎来进行的.不同的存储引擎存储限制不同,支持不同的索引机制等. 查询数据库支持的存储引擎 MySQL 5.7.2支持的存储引擎有:InnoDB ...

  4. Docker最全教程之树莓派和Docker(十五)

    前言 树莓派(Raspberry Pi)是一台卡片电脑(只有信用卡大小),我们可以使用树莓派做很多事情,比如智能家居的中控.航空器.BT下载器.挖矿机.智能机器人.小型服务器(花生壳+网站)等等. 目 ...

  5. [转]Node.js 应用:Koa2 使用 JWT 进行鉴权

    本文转自:https://www.cnblogs.com/linxin/p/9491342.html 前言 在前后端分离的开发中,通过 Restful API 进行数据交互时,如果没有对 API 进行 ...

  6. App.config自定义节点读取

    <?xml version="1.0" encoding="utf-8"?> <configuration> <!--<ot ...

  7. windows系统 docker + swoole 操作

    之前文章讲过在windows系统安装 cgywin 从而安装swoole扩展的操作,但是安装cgywin的时候会出现这样那样的问题.所以今天记录下另一种方式使用docker 安装 swoole 环境! ...

  8. 20190421-那些年使用过的CSS预处理器(CSS Preprocessor)之Sass and Less

    写在前面乱七八糟的前言: emmm,还是决定把Sass与Less单独出来写成一篇,可能会稍微好辣么一丢丢?TAT语法特性是真的香,通篇下来能吸收个10%自我感觉已经很nice了,毕竟渣渣的我有渣渣的自 ...

  9. thinkphp5路由心得

    路由的作用:1. 简化URL地址,方便大家记忆2. 有利于搜索引擎的优化,比如可以被百度的爬虫抓取到 优化URl1. 前后端分离修改入口文件,在public下新建admin.php文件,将下面的代码添 ...

  10. Python笔记-高阶函数

    1.函数式编程 函数式编程就是一种抽象程度很高的编程范式,纯粹的函数式编程语言编写的函数没有变量. 函数式编程的一个特点就是,允许把函数本身作为参数传入另一个函数,还允许返回一个函数! 传入函数 既然 ...