蛮力、分治、动态规划求解最大字段和问题(aardio)

最近的算法课上要求做的一个实验是分别用蛮力、分治、动态规划求解最大字段和问题。
以下是相关代码:

数组求和程序段:

 var getsum = function(tab,frist,last){
    var sum = 0;
    if(type(tab) == type.table){
        if(frist == null){
            frist = 1;
        }
        if(last == null){
            last = table.len(tab);
        }
        for(i = frist;last;1){
            sum += tab[i];
        }
    }       
 return sum;
}

蛮力法求解代码段:

var getByBF = function(tab){
    var count = 0;
    console.log("蛮力法");
    var length = table.len(tab);
    var i = 1;
    var sum = 0;
    var temp;
    var index1,index2;
    console.log("i","j","sum"); 
    for(i=1;length-1;1){
        for(j=i;length;1){
            temp = getsum(tab,i,j);
            if(sum < temp){
                sum = temp;
                index1 = i;
                index2 = j;
            }
            count++;
            console.log(i,j,sum);   
        }   
    }
    console.log("count",count);
    return sum,index1,index2;   
}

分治法求解代码段:

var getByDC = function(tab){
    var count = 0;
    console.log("动态规划");
    var maxSum;
    console.log("frist","last","sum");
    maxSum = function(tab,frist,last){
        if(frist == last){
            count++;
            return tab[frist];
        }
        else {
            var center = math.floor((frist+last)/2);//之前这里是math.ceil导致了栈溢出,是因为当frist为1,last为2时,将导致center也为2从而无穷循环下去
            var leftsum = maxSum(tab,frist,center);
            var rightsum = maxSum(tab,center+1,last);
            var s1 = 0;
            var lefts = 0;
            for(i=center;frist;-1){
                lefts+=tab[i];
                if(lefts >s1){
                    s1 = lefts;
                }
            }
            var s2 = 0;
            var rights = 0;
            for(i=center+1;last;1){
                rights+= tab[i];
                if(rights>s2){
                    s2 = rights;
                }
            }
            var sum = s1 + s2;
            if(sum < leftsum){
                sum = leftsum;
            }
            if(sum < rightsum){
                sum = rightsum;
            }
            count++;
            console.log(frist,last,sum);
            return sum;
        }
    }
    var result = maxSum(tab,1,table.len(tab));
    console.log("count",count);
    return result;
}

动态规划代码段:

var getByDP = function(tab){
    var count = 0;
    console.log("动态规划:");
    var sum = 0;
    var temp = tab[1];
    console.log("i","j","sum");
    for(i=2;table.len(tab);1){
        if(temp > 0){
            temp += tab[i];
        }
        else {
            temp = tab[i];
        }
        console.log(i,temp,sum);
        if(temp > sum){
            sum = temp;
        }
        count++;
    }
    console.log("count",count);
 return sum;
}

随机数组代码段:

var getRandomTab = function(num,min,max){
    var tab = {};
    for(i=1;num;1){ table.push(tab,math.random(min,max)); }
    return tab;
}

主代码段:

import console; 
var taba = getRandomTab(10,-5,10);
console.dump(taba);
console.log("蛮力法:",getByBF(taba));
console.log("分治法:",getByDC(taba));
console.log("动态规划法:",getByDP(taba));
console.pause(true);
    原文作者:动态规划
    原文地址: https://blog.csdn.net/qq_26772049/article/details/72330178
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞