一维数组单步运算
package danbushuzu;
import java.util.Scanner;
public class danBu {
public static void main(String[] args) throws InterruptedException {
Scanner sc = new Scanner(System.in);
//时间复杂度没有实现,只是实现了输出最大值
int n=0;
int linshin=0;
int nshu;//循环的次数
int shu[] = {-1,3,-6,-5,-2};
int max = shu[0];//存储最大的和
int linshimax=1;
System.out.println("0顺序输出,其他数字为指定输出,请输入");
for(nshu=0;nshu<5;nshu++) {
int n1 = 0;
int n2 = 0;
for(int nnshu=nshu;nnshu<5;nnshu++) {
int type = sc.nextInt();
if(type==0) {
n1 = n1 + shu[nnshu];
int zongmax=0;
if(nnshu<4) {
zongmax=max;
nnshu = nnshu + 1;
n2 = n1 + shu[nnshu];
max = maxxx(n1,n2,max);
nnshu = nnshu - 1;
}else {
zongmax=max;
max = maxx(n1,max);
}
linshin++;
System.out.println("当前计算到第" + linshin + "个子数组");
if(zongmax!=max) {
System.out.println("最大子数组为第" + linshin + "个子数组");
linshimax=linshin;
}else {
System.out.println("最大子数组为第" + linshimax + "个子数组");
}
System.out.println("当前运行到第" + (nshu+1) + "个到第" + (nnshu+1) + "个");
System.out.println("最大值为:" + max);
Thread.sleep(1000);
}else {
linshin=0;
n=type;
for(nnshu=nshu;nnshu<5;nnshu++) {
n1 = n1 + shu[nnshu];
int zongmax=0;
if(nnshu<4) {
zongmax=max;
nnshu = nnshu + 1;
n2 = n1 + shu[nnshu];
max = maxxx(n1,n2,max);
nnshu = nnshu - 1;
}else {
zongmax=max;
max = maxx(n1,max);
}
linshin++;
if(linshin==n) {
System.out.println("当前计算到第" + linshin + "个子数组");
if(zongmax==max) {
System.out.println("最大子数组为第" + linshimax + "个子数组");
}else {
System.out.println("最大子数组为第" + linshin + "个子数组");
}
System.out.println("当前运行到第" + (nshu+1) + "个到第" + (nnshu+1) + "个");
System.out.println("最大值为:" + max);
Thread.sleep(1000);
break;
}
}
}
}
}
}
static int maxxx(int a,int b,int ab) {
int max;
if(a<b) {
max = b;
if (max<ab) {
max = ab;
}
}else {
max = a;
if(max<ab) {
max = ab;
}
}
return max;
}
static int maxx(int a , int b){
int max;
if(a<b) {
max = b;
}else {
max = a;
}
return max;
}
}
public static void main(String[] args) throws InterruptedException {
Scanner sc = new Scanner(System.in);
//时间复杂度没有实现,只是实现了输出最大值
int n=0;
int linshin=0;
int nshu;//循环的次数
int shu[] = {-1,3,-6,-5,-2};
int max = shu[0];//存储最大的和
int linshimax=1;
System.out.println("0顺序输出,其他数字为指定输出,请输入");
for(nshu=0;nshu<5;nshu++) {
int n1 = 0;
int n2 = 0;
for(int nnshu=nshu;nnshu<5;nnshu++) {
int type = sc.nextInt();
if(type==0) {
n1 = n1 + shu[nnshu];
int zongmax=0;
if(nnshu<4) {
zongmax=max;
nnshu = nnshu + 1;
n2 = n1 + shu[nnshu];
max = maxxx(n1,n2,max);
nnshu = nnshu - 1;
}else {
zongmax=max;
max = maxx(n1,max);
}
linshin++;
System.out.println("当前计算到第" + linshin + "个子数组");
if(zongmax!=max) {
System.out.println("最大子数组为第" + linshin + "个子数组");
linshimax=linshin;
}else {
System.out.println("最大子数组为第" + linshimax + "个子数组");
}
System.out.println("当前运行到第" + (nshu+1) + "个到第" + (nnshu+1) + "个");
System.out.println("最大值为:" + max);
Thread.sleep(1000);
}else {
linshin=0;
n=type;
for(nnshu=nshu;nnshu<5;nnshu++) {
n1 = n1 + shu[nnshu];
int zongmax=0;
if(nnshu<4) {
zongmax=max;
nnshu = nnshu + 1;
n2 = n1 + shu[nnshu];
max = maxxx(n1,n2,max);
nnshu = nnshu - 1;
}else {
zongmax=max;
max = maxx(n1,max);
}
linshin++;
if(linshin==n) {
System.out.println("当前计算到第" + linshin + "个子数组");
if(zongmax==max) {
System.out.println("最大子数组为第" + linshimax + "个子数组");
}else {
System.out.println("最大子数组为第" + linshin + "个子数组");
}
System.out.println("当前运行到第" + (nshu+1) + "个到第" + (nnshu+1) + "个");
System.out.println("最大值为:" + max);
Thread.sleep(1000);
break;
}
}
}
}
}
}
static int maxxx(int a,int b,int ab) {
int max;
if(a<b) {
max = b;
if (max<ab) {
max = ab;
}
}else {
max = a;
if(max<ab) {
max = ab;
}
}
return max;
}
static int maxx(int a , int b){
int max;
if(a<b) {
max = b;
}else {
max = a;
}
return max;
}
}