返回一个整数数组中最大子数组的和
package t; import java.util.Scanner; public class shuzu_max { public static void main(String args[]) { Scanner scan=new Scanner(System.in); System.out.println("请输入数组的长度"); int n=scan.nextInt(); int [] a=new int [n]; int [] b=new int [n]; int i; System.out.println("请输入数组"); for(i=0;i<n;i++) { a[i]=scan.nextInt(); b[i]=a[i]; } System.out.print(a[0]); for(i=1;i<n;i++) { if(a[i]+a[i-1]>=a[i]) { a[i]=a[i]+a[i-1]; System.out.print(" "+a[i]); }else { System.out.print(" *"); } } int ans=-100000; for(i=0;i<n;i++) { if(ans<a[i]) { ans=a[i]; } } System.out.println(""); System.out.println("最大为"+ans); } }
运行结果测试: