当前位置:网站首页>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;
}
运行结果:
边栏推荐
- 06. Libavdevice Library of ffmpeg
- DOM在Ahooks中的处理过程
- Introduction to kubernetes
- Purpose of setting novice task period in the integral system
- STM32 Development Notes 121: Kalman filter I understand
- Oracle split branches
- STL notes (VIII): container - List
- LeetCode第302场周赛
- Add click event to unity 3D object
- Implementation principle of epoll
猜你喜欢

一篇文章带你读懂Redis的哨兵模式
[email protected]研发效能度量指标"/>学习记录[email protected]研发效能度量指标

Thesis reading | which is the best multilingual pre training technology for machine translation? See the latest progress!

Solution of win11 blue screen code 0x0000001a

Xiaohongshu joins hands with HMS core to enjoy HD vision and grow grass for a better life

An article takes you to understand the sentinel mode of redis
Set up private CA server

Single sign on (one sign on, available everywhere)

What should testers do if they encounter a bug that is difficult to reproduce?

Introduction to kubernetes
随机推荐
Performance Optimization: lazy loading of pictures
Uniapp custom application exit execution content
Zhanrui's first 5g baseband chip was officially released and successfully ranked among the first tier of 5g!
Set up private CA server
STL notes (VIII): container - List
Unity LOD
微服务 - Hystrix 熔断器
06. Libavdevice Library of ffmpeg
VPP不能加载UP状态接口
HMS core discovery Episode 16 live broadcast preview | play AI's new "sound" state with tiger pier
harbor安装
STL notes (IV): String
38 lines of PHP code free import database analysis Linux access log
rhcsa暑假第二天
ZTE's first 5g mobile phone, axon 10 pro, was launched in the first half of this year
JS common code questions array de duplication - Custom New throttling and anti shake - deep copy - instanceof URL parameter extraction - thousand separator - array to tree structure - array flattening
The third day of rhcsa summer vacation
Solve the problem that uni app applet obtains routes and route parameters
Leetcode 15: sum of three numbers
Panda3D keyboard moving scene