Blue Bridge cup vip quiz system - an array of sum (problem-solving ideas and problem-solving codes, hand-drawn mind map though ugly)

Resource constraints

	时间限制:1.0s   内存限制:256.0MB

Problem Description

  输入n个数,围成一圈,求连续m(m<n)个数的和最大为多少?

Input Format

  输入的第一行包含两个整数n, m。第二行,共n个整数。

Output Format

  输出1行,包含一个整数,连续m个数之和的最大值。

Sample input

	10 3
	9 10 1 5 9 3 2 6 7 4

Sample Output

	23

Scale data and conventions

  0<m<n<1000, -32768<=输入的每个数<=32767

Here Insert Picture Description

Submit code


import java.util.*;

public class Main {
    static Scanner sc = new Scanner(System.in);

    public static void main(String[] args) {
        int n = sc.nextInt();//n个数
        int m = sc.nextInt();//求m个连续的数的最大和
        int number[] = new int[n];
        for (int i = 0; i < n; i++) {
            number[i] = sc.nextInt();
        }

        List<Integer> list = new ArrayList<>();

        int numb [] = new int[number.length+m];//形成一个环
        //拷贝数组
        System.arraycopy(number, 0, numb, 0, number.length);
        for (int i = 0; i < m; i++) {
            numb[(number.length+i)] = number[i];
        }
        for (int i = 0; i < numb.length; i++) {
            if((i+m)<=numb.length){

                int num = 0;
                for (int j = i; j < m+i; j++) {
                    num = num + numb[j];
                }
                list.add(num);
            }
        }
        Object[] objects = list.toArray();
        Arrays.sort(objects);
        System.out.print((int)objects[objects.length-1]);
    }
}

Mind map

Here Insert Picture Description

Published 152 original articles · won praise 577 · views 80000 +

Guess you like

Origin blog.csdn.net/qq_17623363/article/details/105000133