【好未来20180828Java】给定一个正整数数组,找出和最大的子数组(升序),输出和

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014067137/article/details/82154812
12345 能被3整除的个数?

12 3 45    3个
123 45    2个

 
package test0828;

import java.util.Scanner;

/**
 * @author wyl
 * @time 2018年8月28日下午8:12:07
 */
public class Main3 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner scanner=new Scanner(System.in);
		char[] cs=scanner.nextLine().toCharArray();
		int res=0;
		int i=0;
		int tmp=0;
		while(i<cs.length){
			if ((cs[i]-'0')%3==0) {
				res++;
				i++;
			}else {
				tmp=cs[i]-'0';
				i++;
				while(i<cs.length){
					tmp=tmp*10+cs[i]-'0';
					if ((cs[i]-'0')%3==0||tmp%3==0) {
						res++;
						i++;
						break;
					}
					i++;
				}
			}
		}
		System.out.println(res);
	}

}
给定一个正整数数组,找出和最大的子数组(升序),输出和

如 5 1 3 4 9 7 6 8
 输出 23


所有可能: 5  9==14
          1 3 4 7 8=23
package test0828;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * @author wyl
 * @time 2018年8月28日下午8:48:12
 */
public class Main4 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner scanner=new Scanner(System.in);
		while(scanner.hasNext()){
			int num=scanner.nextInt();
			int[] arr=new int[num];
			for(int i=0;i<num;i++){
				arr[i]=scanner.nextInt();
			}
			System.out.println(sum(arr,num));
		}
		scanner.close();
	}

	private static int sum(int[] arr, int num) {
		// TODO Auto-generated method stub
		int max=0;
		int res=0;
		ArrayList<Integer> list2=new ArrayList<>();
		
		for(int i=0;i<num;i++){
			list2.add(arr[i]);
			while(arr[++i]>arr[i]) {
				list2.add(arr[i]);
			}
			for(int j=0;j<list2.size();j++){
				res+=list2.get(j);
			}
			if (res>max) {
				max=res;
			}
			res=0;
		}
		
		return max;
	}

}

猜你喜欢

转载自blog.csdn.net/u014067137/article/details/82154812
今日推荐