首先膜拜算法大神,然后无脑穷举一下。
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner =new Scanner(System.in);
        int N=scanner.nextInt();
        List<Integer> number=new ArrayList<Integer>();
        int sum=0,result=0;
        for(int i=0;i<N;i++){
            number.add(scanner.nextInt());
        }
        int P=scanner.nextInt();
        for(int i=0;i<N;i++){
            for(int j=i;j<N;j++){
                sum+=number.get(j);
                if(sum%P==0){
                result=result>j-i+1?result:j-i+1;
                }
            }
            if(result>N-i)//当前最长比剩余数字个数大,不做了
                break;//不跳出会超时,0.83
            sum=0;
         }
        System.out.println(result);
    }
}