Design ideas:
- Randomly generated arrays (with positive and negative numbers).
- All sub-arrays: The array is divided by the length of the array (because the array is required to be contiguous).
- Calculation: From the first number to do the addition, if and greater than the second number, then continue to add the next number (if the sum of the third number and greater than the third number, then repeat the operation; less than the third number, and then give up the previous, and continue to do this); if it is less than the second number, the first number.
Implementation code:
#include <iostream>
#include <time.h> using namespace std;
int Maxsum (int* A, int n) {
int sum=0;
In fact, to deal with the situation is full of negative, very simple, as the following 3rd see below, directly to change this sentence: "int sum=a[0]" can
can also not change, when full of negative conditions, directly return 0, also not necessarily not.
int b=0;
for (int i=0; i<n; i++) {
if (b<0)
B=a[i];
Else
B+=a[i];
if (sum<b)
Sum=b;
}
return sum;
}
int main () {
int n=1000,k=0;
int *a=new int [n];
for (int i = 0; i < n; i++) {
A[i] = rand ()%20-10;
} for (int i = 0; i < n; i++) {
cout<<a[i]<< "";
}
for (int i = 0; i < n; i++)//When the resulting array is negative
if (a[i]<0) {
k++;
}
}
if (n==k) {
for (int i = 1; i < n; i++) {
K=A[0];
if (a[i]>k) {
K=a[i];
}
} C
out<< "Max and as:" <<k<<endl;
}
else {
cout<< "Max and for:" <<maxsum (a,n) <<endl;
}
cout<< "Please enter array length:" <<endl; cin>>n;
cout<< "Please enter array elements:" <<endl;
for (int i = 0; i < n; i++) {
Cin>>a[i];
}
cout<<endl;
for (int i = 0; i < n; i++)//When the resulting array is negative
if (a[i]<0) {
k++;
}
}
if (n==k) {
for (int i = 1; i < n; i++) {
K=A[0];
if (a[i]>k) {
K=a[i];
}
}
cout<< "Max and as:" <<k<<endl;
}
else {
cout<< "Max and for:" <<maxsum (a,n) <<endl;
}
return 0;
}
Results:
Experiment Summary:
Summary: This experiment is mainly on the basis of the last look at when the output of the array elements of the situation will be what the problem and the value of the array elements when the situation will occur, from the actual point of view the former does not appear, but in the value of a large number of unexpected error when the program is not perfect. And then this experiment is done by two pairs, preliminary to the cooperation with some understanding and sentiment, the two rooms how to assign the task will affect the progress of the whole project, and two people are assigned to what task should be able to let everyone play a specialty, And then the timing of the problem because it is to cooperate for everyone to divide the time according to the division of time is unreasonable also will cause a person busy death a person idle death, obviously this is unreasonable.
Cooperation Photo:
Returns the maximum sum of the number of sub-arrays in a random array and (ii)