当前位置:网站首页>C编程 --“最大子数组的和” 的动态规划的解法
C编程 --“最大子数组的和” 的动态规划的解法
2022-07-25 05:21:00 【IT_阿水】
C编程 --“最大子数组的和” 的动态规划的解法
1.最大子数组之和
例1:数组int a1[5] = { -1, 5, 6, -7, 3 };其最大子数组之和为:5+6=11
例2:数组int a2[5] = { -5, -4, -8, -1, -10 };其最大子数组之和为:-1
例3:数组 int a3[5] = { -1, 5, 6, -7, 10 };其最大子数组之和为:5+6-7+10=14
功能实现:
# include <stdio.h>
# include <string.h>
int MaxSum(int* arr, int size)
{
int current = arr[0]; //当前数组最大和
int max = current;
for (int i = 0; i < size; i++)
{
if (current < 0)
current = 0;
current += arr[i];
if (current > max)
max = current;
}
return max;
}
int main(void)
{
char x[40], y[40];
int a1[5] = {
-1, 5, 6, -7, 3 };
int a2[5] = {
-5, -4, -8, -1, -10 };
int a3[5] = {
-1, 5, 6, -7, 10 };
int max1, max2, max3;
max1 = MaxSum(a1, 5);
max2 = MaxSum(a2, 5); //这个应该返回 -1,
max3 = MaxSum(a3, 5);
printf("max1=%d,max2=%d,max3=%d\n",max1,max2,max3);
}
2.获取最大子数组的开始和结束的下标
如果我需要返回值返回这个最大子数组的开始和结束的下标,你要怎么修改这个程序?
例1:数组int a1[5] = { -1, 5, 6, -7, 3 };其最大子数组之和为:5+6=11;最大子数组开始和结束下标为:1 2。
例2:数组int a2[5] = { -5, -4, -8, -1, -10 };其最大子数组之和为:-1;最大子数组开始和结束下标为:3 3。
例3:数组 int a3[5] = { -1, 5, 6, -7, 10 };其最大子数组之和为:5+6-7+10=14 ; 最大子数组开始和结束下标为:1 4。
例4:数组 int a3[] = {-2, -1, -3, 4, -1, 2, 1, -5, 4};其最大子数组之和为:4+(-1)+2+1=6 ; 最大子数组开始和结束下标为:3 6。
功能实现:
#include <stdio.h>
#include <stdlib.h>
void solution(int m, int *arr){
int current=arr[0];
int max=current;
int start=0,end=0;
int i=0;
/*计算最大子数组之和*/
for(i=1;i<m;i++)
{
if (current < 0)current = 0;
current += arr[i];
if(current>max)
{
max = current;
end=i;//最大子数组结束下标
}
}
int temp=max;
/*计算最大子数组结束下标*/
for(i=end;i>=0;i--)
{
temp-=arr[i];
if(temp<=0 || temp>max)break;
}
if(i<0)i=0;
start=i;
printf("%d,%d %d\n",max,start,end);
}
int main() {
int n;
printf("输入个数:");
scanf("%d", &n);
int *arr;
arr = (int*)malloc(n * sizeof(int));
printf("输入%d个整数:",n);
for (int i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
solution(n, arr);
return 0;
}
运行结果:
边栏推荐
猜你喜欢

STL notes (III): input / output stream

一篇文章带你读懂Redis的哨兵模式
[email protected] R & D effectiveness measurement indicators"/>Learning records [email protected] R & D effectiveness measurement indicators

RHCE first day

Solve the problem that uni app applet obtains routes and route parameters
![2022-07-24:以下go语言代码输出什么?A:[]int{};B:[]int(nil);C:panic;D:编译错误。 package main import ( “fmt“ ) f](/img/bf/e38a8fd813f88a83f61a1abfa3b95d.png)
2022-07-24:以下go语言代码输出什么?A:[]int{};B:[]int(nil);C:panic;D:编译错误。 package main import ( “fmt“ ) f

JWT(json web token)
[small program practice] first day

Pikachu vulnerability platform exercise

Implement is by yourself_ class
随机推荐
自己实现is_class
Baklib: share some methods about building enterprise knowledge management (km)
Deep error
Use getifaddrs to obtain the IP address of the local network interface
Redis的三个必知必会的问题
Matter's Unified Smart Home connection standard enables local automatic interaction between smart devices
85 distributed project construction
In depth understanding of pre post + +, -- and negative modulus
The global shipment of glory 8x series exceeded 10million units, and the glory V20 exceeded 1.5 million units!
服务器防护的七个建议
Learning records [email protected] R & D effectiveness measurement indicators
2022-07-24:以下go语言代码输出什么?A:[]int{};B:[]int(nil);C:panic;D:编译错误。 package main import ( “fmt“ ) f
[the most comprehensive and detailed] how to design a database and table splitting scheme that can dynamically expand and shrink capacity?
STL notes (I): knowledge system
[untitled]
Implement is by yourself_ convertible
Guanghetong and Intel released the global version of 5g communication module
弹性布局总结
剑指offer专项突击版第9天
教你三招从让性能从20s优化到500ms