应该是动态规划吧 但是我不会动态规划 就用遍历做的
反正感觉写的很垃圾 但是输出好像是对的
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
int devide(int* array, int len, int m, int* round, int* pos, int begin, int* sum,int m2,int* final)
{
int i;
if (m2 == 0)
{
int j = 0;
int* subsum = new int[m ];
memset(subsum, 0, sizeof(int) * (m));
for (int k = 0;k <= pos[1];k++)
{
subsum[0] = subsum[0] + array[k];
}
for (j = 1;j < m-1 ;j++)
{
for (int k = pos[j]+1;k < pos[j+1]+1;k++)
{
subsum[j] = subsum[j] + array[k];
}
}
for (int k = pos[m-1];k < len-1;k++)
{
subsum[m-1] = subsum[m-1] + array[k+1];
}
int subsubsum = 100000;
for (int s = 0;s < m - 1;s++)
{
if (subsubsum > subsum[s])
{
subsubsum = subsum[s];
}
}
if (subsubsum > *sum)
{
*sum = subsubsum;
for (int s = 1;s < m ;s++)
{
final[s] = pos[s];
}
}
delete[] subsum;
return 1;
}
for (i = begin;i < len - m + 1;i++)
{
begin = begin +1;
pos[(*round)+1] = i;
(*round)++;
devide(array, len, m , round, pos, begin,sum,m2-1,final);
(*round)--;
}
return 1;
}
int main()
{
int len = 9;
int array[9] = { 1,2,3,4,5,6,7,8,9 };
int m;
m =3;
int round = 0;
int* result=new int[m];
result[0] = 0;
int sum = 0;
int begin = 0;
int ret;
int* final = new int[m];
final[0] = 0;
ret = devide(array, len, m, &round, result, begin, &sum,m-1,final);
cout << sum << endl;
for (int s = 1;s < m;s++)
{
cout << final[s] << endl;
}
return ret;
}
输出 13 4 6