假定给定的一组数为:-2,-4,-7,-20,1,1,1,1,-10,1,1,1,5,-10,10,10,10,-25,10,10,10,10,10,10,-300
算法要求:在给定的数组中找出某个连续子串,该子串的值是最大的,并且输出该子串和最大值。
代码如下:
public class Search {
static int max=0;
static int max1=0;
static int [] n = {-2,-4,-7,-20,1,1,1,1,-10,1,1,1,5,-10,10,10,10,-25,10,10,10,10,10,10,-300};
static String tempStr = "";
static String maxStr = "";
static int temp;
static String Find() {
for(int i = 0;i <25; i++) {
temp += n[i];
tempStr += n[i]+",";
if(temp >= max) {
maxStr = tempStr;
}
if(temp < 0) {
temp = 0;
tempStr = "";
}