06返回一个数组中最大和

设计思想:

  设数组长maxLength;

  设子数组大小(length),依次为1,2,3,4,5........maxLength,并求每一个子数组的和

  比较子数组和大小,求最大子数组和;

源代码:

import java.util.Vector;

public class MaxListNumber {

public static int maxList(int []num) {

int max=-99;

if(num.length<=0) {

System.out.println("数组为空。");

}

else {

Vector<Integer> numx=new Vector<>();

for(int i=1;i<num.length;i++) {

System.out.println("数组元素:"+num[i]);

for(int j=0;j<num.length;j++) {

int count=j;

int sum=0;

while(count-j<i&&count<num.length) {

sum=sum+num[count];

count++;

}

numx.add(sum);

}

}

max=numx.get(0);

for(int i=0;i<numx.size();i++) {

if(max<numx.get(i)) {

max=numx.get(i);

}

}

}

return max;

}

public static void main(String args[]) {

int number[]=new int[10];

for(int i=0;i<10;i++) {

number[i]=(int) (Math.random()*10-5);

}

int max=maxList(number);

System.out.println("最大子数组的和:"+max);

}

}

原文地址:https://www.cnblogs.com/liushiqiang123/p/8349775.html

时间: 2024-08-10 10:11:31

06返回一个数组中最大和的相关文章

面试题--&gt;写一个函数,返回一个数组中所有元素被第一个元素除的结果

1 package com.rui.test; 2 3 import java.util.Random; 4 5 /** 6 * @author poseidon 7 * @version 1.0 8 * @date:2015年10月25日 上午11:12:24 9 * @description: 面试题-->写一个函数,返回一个数组中所有元素被第一个元素除的结果 10 * 陷阱: 11 * 1.循环需要倒着写,为什么? 12 * 想想顺着写循环的结果,第一个元素的值会发生变化门后面再除就会有问

输出一个数组中最大和的子数组并且和不溢出

组员 石鹤李海超 一.题目 输出一个数组中最大和的子数组,数组为int32型,有1000个数,保证最大子数组的和不溢出. 二.设计思想 随机产生一千个数的数组,求最大子数组和的过程中判断是否溢出,如果溢出,则输出溢出. 三.源代码 #include<iostream.h> long MAX(unsigned long arry[],int length) { unsigned long sum=arry[0]; unsigned long maxsum=arry[0]; for(int i=1

[编程之美]写一个函数,返回一个数组中所有元素被第一个元素除的结果

题目: 写一个函数,返回一个数组中所有元素被第一个元素除的结果: 错误的版本: void DivArray(int *array, int n) { for (int i = 0; i < n; ++i) { array[i] /= array[0]; } } 错误原因:在循环的第一步,第一个元素就变成了1,然后再用它去除以其他元素,就不符合题目要求了 改进: 1:使用其他变量保存第一个元素: void DivArray3(int *array, int n) { assert(array !=

返回一个数组中最大子数组的和

public class test { public static void main(String[] args) { int[] a = new int[10]; int[] b=new int [10]; int j=0; int sum=0; a[0] = 1; a[1] = -1; a[2] = 2; a[3] = 3; a[4] = 4; a[5] = -5; a[6] = 6; a[7] = -8; a[8] = 9; a[9] = 10; for(int i=0;i<10;i++

给定一个只包含正整数的非空数组,返回该数组中重复次数最多的前N个数字 ,返回的结果按重复次数从多到少降序排列(N不存在取值非法的情况)

1 """ 2 #给定一个只包含正整数的非空数组,返回该数组中重复次数最多的前N个数字 3 #返回的结果按重复次数从多到少降序排列(N不存在取值非法的情况) 4 解题思路: 5 1.设定一个空字典,去存储列表中的值和值出现的次数 6 2.使用L.count()方法可以统计出L中值出现的次数 7 3.使用sorted方法可以进行排序,sorted(iterable,key,reverse) 8 注意key是函数 9 4.列表中的元祖取值 d[i][j] i是哪一个元祖,j是元祖

C++_第七章函数的基本知识_求阶乘的子函数_ 函数参数类型为数组_ 求数组内所有元素和、部分元素和的方法_实现了先从键盘输入到一个数组中,再用for循环取读出数组中的元素 for循环也可以用break来结束循环的

/* 第七章函数的基本知识 */ /*01)c++对于返回值有一定的限制:可以是常量.变量.指针.结构对象或表达式,但不可以是数组02)c++返回数组的方法:将数组作为结构会对象组成部分来返回03)函数遇到return则结束该函数04)如果一个函数的两房额参数类型相同,则必须分别制定每个参数的类型,而不能像声明常规变量那样,将声明组合在一起05)*/ //本代码注意double类型的写法以及double和int类型数据的转换 1 #include <iostream> 2 3 void che

【转载】让c++ 函数返回一个数组

在c++中是不允许数组作为函数的返回值的 int [] someFunction( ); //ILLEGAL 要想实现函数返回一个数组,那返回对应数组里面类型的指针 you must return a pointer to the array base type and have the pointer point to the array. So, the function declaration would be as follows: int* someFunction( ); //Leg

数组中 最大和 的子数组

题目: 输入一个整型数组,数据元素有正数也有负数,求元素组合成连续子数组之和最大的子数组,要求时间复杂度为O(n). 例如: 输入的数组为1, -2, 3, 10, -4, 7, 2, -5,最大和的连续子数组为3, 10, -4, 7, 2,其最大和为18. 背景: 本题最初为2005年浙江大学计算机系考研题的最后一道程序设计题,在2006年里包括google在内的很多知名公司都把本题当作面试题. 由于本题在网络中广为流传,本题也顺利成为2006年程序员面试题中经典中的经典. 分析: 如果不考

巧妙利用快速排序法的原理求一个数组中的第10大元素

//快速排序法 int QuickSort_process3(int *a, int low, int high) { int l, h, temp; l = low; h = high; temp = a[low]; while (l < h){ while (l< h&&a[h] >= temp) --h; if (l < h) a[l] = a[h]; while (l < h&&a[l] < temp) ++l; if (l &l