ALGORITHM 4.1-2

来源:互联网 发布:linux怎么进入vim 编辑:程序博客网 时间:2024/05/14 15:06

CONSOLE APPLICATION 3

analyse problem


Proposal max-subarray-problem [O(n^2)]

brute-force algorithm:

#include "stdafx.h"#include<iostream>#include<stdio.h>using namespace std;//brute-force algorithmint main(){    int n = 0, sum = 0;    int q = 0, w = 0, e = 0, r = 0;    cout << "this this brute force\t\n";    cout << "input the array size\n";    cin >> n;    int*maxarray = new int[n];    int i = 0;    cout << "please input array\n";    for (; i < n; i++)    {        cin >> maxarray[i];    }    int tmp = 0,maxtmp=0;    for (i = 0; i < n; i++)    {        for (w = i + 1; w < n; w++)        {            for (e = i; e < w; e++)            {                sum = 0;                sum += maxarray[e];            }            tmp = sum;            if (tmp > maxtmp)                maxtmp = tmp;        }    }    cout << "the max subarray" << maxtmp << endl;    return 0;}

But now,I can’t analyse this algorithm.

what else I also can’t figure out the divide-conquer way.

原创粉丝点击