View Code of Problem 19

import java.util.Scanner;

public class MaxSum {
	int a[]=new int [100];//记录序列
	int b[]=new int [10000];//记录序列的各个子串的和
	int length=0;
	
	void ShuRu(){//向数组a中输入内容
		Scanner scan=new Scanner(System.in);
		System.out.println("序列的长度为:");
		length=scan.nextInt();
		System.out.println("请输入序列:");
		for(int i=0;i<length;i++){
			a[i]=scan.nextInt();
		}
		System.out.println("序列为:");
		for(int i=0;i<length;i++){
			System.out.print(a[i]+" ");
		}
	}
	
	int sum(int i, int j){//求a[i]+a[i+1]+....+a[j]
		int m=0;
		while(i!=j+1){
			m=m+a[i];
			i++;
		}
		return m;
	}
	
	int Max(int a[]){
		int n=0;
		for(int i=0;i<length-1;i++){
			for(int j=i+1;j<length;j++){	
				b[n]=this.sum(i, j);
				n++;
			}
		}
		for(int i=0;i<length;i++){//将数组a中大于零的项记入数组b
			if(a[i]>=0){
				b[n]=a[i];
				n++;
			}
		}
		int m=a[0];
		for(int i=0;i<n;i++){//求数组b中的最大项
			if(b[i]>m){
				m=b[i];
			}
		}
		System.out.println("最大子串和为:"+m);
		return m;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		MaxSum MS=new MaxSum();
		MS.ShuRu();
		MS.Max(MS.a);
	}

}

/*
Main.java:4: error: unmappable character for encoding ASCII
	int a[]=new int [100];//????????????
	                        ^
Main.java:4: error: unmappable character for encoding ASCII
	int a[]=new int [100];//????????????
	                         ^
Main.java:4: error: unmappable character for encoding ASCII
	int a[]=new int [100];//????????????
	                          ^
Main.java:4: error: unmappable character for encoding ASCII
	int a[]=new int [100];//????????????
	                           ^
Main.java:4: error: unmappable character for encoding ASCII
	int a[]=new int [100];//????????????
	                            ^
Main.java:4: error: unmappable character for encoding ASCII
	int a[]=new int [100];//????????????
	                             ^
Main.java:4: error: unmappable character for encoding ASCII
	int a[]=new int [100];//????????????
	                              ^
Main.java:4: error: unmappable character for encoding ASCII
	int a[]=new int [100];//????????????
	                               ^
Main.java:4: error: unmappable character for encoding ASCII
	int a[]=new int [100];//????????????
	                                ^
Main.java:4: error: unmappable character for encoding ASCII
	int a[]=new int [100];//????????????
	                                 ^
Main.java:4: error: unmappable character for encoding ASCII
	int a[]=new int [100];//????????????
	                                  ^
Main.java:4: error: unmappable character for encoding ASCII
	int a[]=new int [100];//????????????
	                                   ^
Main.java:5: error: unmappable character for encoding ASCII
	int b[]=new int [10000];//?????????????????????????????????
	                          ^
Main.java:5: error: unmappable character for encoding ASCII
	int b[]=new int [10000];//?????????????????????????????????
	                           ^
Main.java:5: error: unmappable character for encoding ASCII
	int b[]=new int [10000];//?????????????????????????????????
	                            ^
Main.java:5: error: unmappable character for encoding ASCII
	int b[]=new int [10000];//?????????????????????????????????
	                             ^
Main.java:5: error: unmappable character for encoding ASCII
	int b[]=new int [10000];//?????????????????????????????????
	                              ^
Main.java:5: error: unmappable character for encoding ASCII
	int b[]=new int [10000];//?????????????????????????????????
	                               ^
Main.java:5: error: unmappable character for encoding ASCII
	int b[]=new int [10000];//?????????????????????????????????
	                                ^
Main.java:5: error: unmappable character for encoding ASCII
	int b[]=new int [10000];//?????????????????????????????????
	                                 ^
Main.java:5: error: unmappable character for encoding ASCII
	int b[]=new int [10000];//?????????????????????????????????
	                                  ^
Main.java:5: error: unmappable character for encoding ASCII
	int b[]=new int [10000];//?????????????????????????????????
	                                   ^
Main.java:5: error: unmappable character for encoding ASCII
	int b[]=new int [10000];//?????????????????????????????????
	                                    ^
Main.java:5: error: unmappable character for encoding ASCII
	int b[]=new int [10000];//?????????????????????????????????
	                                     ^
Main.java:5: error: unmappable character for encoding ASCII
	int b[]=new int [10000];//?????????????????????????????????
	                                      ^
Main.java:5: error: unmappable character for encoding ASCII
	int b[]=new int [10000];//?????????????????????????????????
	                                       ^
Main.java:5: error: unmappable character for encoding ASCII
	int b[]=new int [10000];//?????????????????????????????????
	                                        ^
Main.java:5: error: unmappable character for encoding ASCII
	int b[]=new int [10000];//?????????????????????????????????
	                                         ^
Main.java:5: error: unmappable character for encoding ASCII
	int b[]=new int [10000];//?????????????????????????????????
	                                          ^
Main.java:5: error: unmappable character for encoding ASCII
	int b[]=new int [10000];//?????????????????????????????????
	                                           ^
Main.java:5: error: unmappable character for encoding ASCII
	int b[]=new int [10000];//?????????????????????????????????
	                                            ^
Main.java:5: error: unmappable character for encoding ASCII
	int b[]=new int [10000];//?????????????????????????????????
	                                             ^
Main.java:5: error: unmappable character for encoding ASCII
	int b[]=new int [10000];//?????????????????????????????????
	                                              ^
Main.java:5: error: unmappable character for encoding ASCII
	int b[]=new int [10000];//?????????????????????????????????
	                                               ^
Main.java:5: error: unmappable character for encoding ASCII
	int b[]=new int [10000];//?????????????????????????????????
	                                                ^
Main.java:5: error: unmappable character for encoding ASCII
	int b[]=new int [10000];//?????????????????????????????????
	                                                 ^
Main.java:5: error: unmappable character for encoding ASCII
	int b[]=new int [10000];//?????????????????????????????????
	                                                  ^
Main.java:5: error: unmappable character for encoding ASCII
	int b[]=new int [10000];//?????????????????????????????????
	                                                   ^
Main.java:5: error: unmappable character for encoding ASCII
	int b[]=new int [10000];//?????????????????????????????????
	                                                    ^
Main.java:5: error: unmappable character for encoding ASCII
	int b[]=new int [10000];//?????????????????????????????????
	                                                     ^
Main.java:5: error: unmappable character for encoding ASCII
	int b[]=new int [10000];//?????????????????????????????????
	                                                      ^
Main.java:5: error: unmappable character for encoding ASCII
	int b[]=new int [10000];//?????????????????????????????????
	                                                       ^
Main.java:5: error: unmappable character for encoding ASCII
	int b[]=new int [10000];//?????????????????????????????????
	                                                        ^
Main.java:5: error: unmappable character for encoding ASCII
	int b[]=new int [10000];//?????????????????????????????????
	                                                         ^
Main.java:5: error: unmappable character for encoding ASCII
	int b[]=new int [10000];//?????????????????????????????????
	                                                          ^
Main.java:8: error: unmappable character for encoding ASCII
	void ShuRu(){//?????????a???????????????
	               ^
Main.java:8: error: unmappable character for encoding ASCII
	void ShuRu(){//?????????a???????????????
	                ^
Main.java:8: error: unmappable character for encoding ASCII
	void ShuRu(){//?????????a???????????????
	                 ^
Main.java:8: error: unmappable character for encoding ASCII
	void ShuRu(){//?????????a???????????????
	                  ^
Main.java:8: error: unmappable character for encoding ASCII
	void ShuRu(){//?????????a???????????????
	                   ^
Main.java:8: error: unmappable character for encoding ASCII
	void ShuRu(){//?????????a???????????????
	                    ^
Main.java:8: error: unmappable character for encoding ASCII
	void ShuRu(){//?????????a???????????????
	                     ^
Main.java:8: error: unmappable character for encoding ASCII
	void ShuRu(){//?????????a???????????????
	                      ^
Main.java:8: error: unmappable character for encoding ASCII
	void ShuRu(){//?????????a???????????????
	                       ^
Main.java:8: error: unmappable character for encoding ASCII
	void ShuRu(){//?????????a???????????????
	                         ^
Main.java:8: error: unmappable character for encoding ASCII
	void ShuRu(){//?????????a???????????????
	                          ^
Main.java:8: error: unmappable character for encoding ASCII
	void ShuRu(){//?????????a???????????????
	                           ^
Main.java:8: error: unmappable character for encoding ASCII
	void ShuRu(){//?????????a???????????????
	                            ^
Main.java:8: error: unmappable character for encoding ASCII
	void ShuRu(){//?????????a???????????????
	                             ^
Main.java:8: error: unmappable character for encoding ASCII
	void ShuRu(){//?????????a???????????????
	                              ^
Main.java:8: error: unmappable character for encoding ASCII
	void ShuRu(){//?????????a???????????????
	                               ^
Main.java:8: error: unmappable character for encoding ASCII
	void ShuRu(){//?????????a???????????????
	                                ^
Main.java:8: error: unmappable character for encoding ASCII
	void ShuRu(){//?????????a???????????????
	                                 ^
Main.java:8: error: unmappable character for encoding ASCII
	void ShuRu(){//?????????a???????????????
	                                  ^
Main.java:8: error: unmappable character for encoding ASCII
	void ShuRu(){//?????????a???????????????
	                                   ^
Main.java:8: error: unmappable character for encoding ASCII
	void ShuRu(){//?????????a???????????????
	                                    ^
Main.java:8: error: unmappable character for encoding ASCII
	void ShuRu(){//?????????a???????????????
	                                     ^
Main.java:8: error: unmappable character for encoding ASCII
	void ShuRu(){//?????????a???????????????
	                                      ^
Main.java:8: error: unmappable character for encoding ASCII
	void ShuRu(){//?????????a???????????????
	                                       ^
Main.java:10: error: unmappable character for encoding ASCII
		System.out.println("?????????????????????");
		                    ^
Main.java:10: error: unmappable character for encoding ASCII
		System.out.println("?????????????????????");
		                     ^
Main.java:10: error: unmappable character for encoding ASCII
		System.out.println("?????????????????????");
		                      ^
Main.java:10: error: unmappable character for encoding ASCII
		System.out.println("?????????????????????");
		                       ^
Main.java:10: error: unmappable character for encoding ASCII
		System.out.println("?????????????????????");
		                        ^
Main.java:10: error: unmappable character for encoding ASCII
		System.out.println("?????????????????????");
		                         ^
Main.java:10: error: unmappable character for encoding ASCII
		System.out.println("?????????????????????");
		                          ^
Main.java:10: error: unmappable character for encoding ASCII
		System.out.println("?????????????????????");
		                           ^
Main.java:10: error: unmappable character for encoding ASCII
		System.out.println("?????????????????????");
		                            ^
Main.java:10: error: unmappable character for encoding ASCII
		System.out.println("?????????????????????");
		                             ^
Main.java:10: error: unmappable character for encoding ASCII
		System.out.println("?????????????????????");
		                              ^
Main.java:10: error: unmappable character for encoding ASCII
		System.out.println("?????????????????????");
		                               ^
Main.java:10: error: unmappable character for encoding ASCII
		System.out.println("?????????????????????");
		                                ^
Main.java:10: error: unmappable character for encoding ASCII
		System.out.println("?????????????????????");
		                                 ^
Main.java:10: error: unmappable character for encoding ASCII
		System.out.println("?????????????????????");
		                                  ^
Main.java:10: error: unmappable character for encoding ASCII
		System.out.println("?????????????????????");
		                                   ^
Main.java:10: error: unmappable character for encoding ASCII
		System.out.println("?????????????????????");
		                                    ^
Main.java:10: error: unmappable character for encoding ASCII
		System.out.println("?????????????????????");
		                                     ^
Main.java:10: error: unmappable character for encoding ASCII
		System.out.println("?????????????????????");
		                                      ^
Main.java:10: error: unmappable character for encoding ASCII
		System.out.println("?????????????????????");
		                                       ^
Main.java:10: error: unmappable character for encoding ASCII
		System.out.println("?????????????????????");
		                                        ^
Main.java:12: error: unmappable character for encoding ASCII
		System.out.println("??????????????????");
		                    ^
Main.java:12: error: unmappable character for encoding ASCII
		System.out.println("??????????????????");
		                     ^
Main.java:12: error: unmappable character for encoding ASCII
		System.out.println("??????????????????");
		                      ^
Main.java:12: error: unmappable character for encoding ASCII
		System.out.println("??????????????????");
		                       ^
Main.java:12: error: unmappable character for encoding ASCII
		System.out.println("??????????????????");
		                        ^
Main.java:12: error: unmappable character for encoding ASCII
		System.out.println("??????????????????");
		                         ^
Main.java:12: error: unmappable character for encoding ASCII
		System.out.println("??????????????????");
		                          ^
Main.java:12: error: unmappable character for encoding ASCII
		System.out.println("??????????????????");
		                           ^
Main.java:12: error: unmappable character for encoding ASCII
		System.out.println("??????????????????");
		                            ^
Main.java:12: error: unmappable character for encoding ASCII
		System.out.println("??????????????????");
		                             ^
100 errors
*/

Double click to view unformatted code.


Back to problem 19