#include using namespace std; /****************************************************************************************/ /*第一种方法,容易想到,时间复杂度为O(n*n) **************************************/ /*传入一数组首地址和数组元素个数,传出最大子序列和,并同时打印输出下标范围*/ /****************************************************************************************/ int Max1(int *data, int n) { int i,j,left = 0, right = 0; int result = 0; int max = data[0]; for (i = 0; i < n; i ++) { result = 0; for (j = i; j < n; j ++) { result += data[j]; if (result > max) { max = result; left = i; right = j; }
} } cout<<"left = "< cout<<"The largest result is :"< cout<<"Second way:"< cout<<"The largest result is :"< return 0; } 运行结果如下: