当前位置:网站首页>C Programming -- the solution of dynamic programming of "the sum of the largest subarray"
C Programming -- the solution of dynamic programming of "the sum of the largest subarray"
2022-07-25 05:24:00 【IT_ Ashui】
C Programming --“ Sum of the largest subarray ” The solution of dynamic programming
1. The sum of the largest subarrays
example 1: Array int a1[5] = { -1, 5, 6, -7, 3 }; The sum of its largest subarray is :5+6=11
example 2: Array int a2[5] = { -5, -4, -8, -1, -10 }; The sum of its largest subarray is :-1
example 3: Array int a3[5] = { -1, 5, 6, -7, 10 }; The sum of its largest subarray is :5+6-7+10=14
Function realization :
# include <stdio.h>
# include <string.h>
int MaxSum(int* arr, int size)
{
int current = arr[0]; // Current array maximum sum
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); // This should return -1,
max3 = MaxSum(a3, 5);
printf("max1=%d,max2=%d,max3=%d\n",max1,max2,max3);
}
2. Get the subscripts of the beginning and end of the largest subarray
If I need to return the value, return the subscript of the beginning and end of the largest subarray , How do you modify this program ?
example 1: Array int a1[5] = { -1, 5, 6, -7, 3 }; The sum of its largest subarray is :5+6=11; The start and end subscripts of the largest subarray are :1 2.
example 2: Array int a2[5] = { -5, -4, -8, -1, -10 }; The sum of its largest subarray is :-1; The start and end subscripts of the largest subarray are :3 3.
example 3: Array int a3[5] = { -1, 5, 6, -7, 10 }; The sum of its largest subarray is :5+6-7+10=14 ; The start and end subscripts of the largest subarray are :1 4.
example 4: Array int a3[] = {-2, -1, -3, 4, -1, 2, 1, -5, 4}; The sum of its largest subarray is :4+(-1)+2+1=6 ; The start and end subscripts of the largest subarray are :3 6.
Function realization :
#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;
/* Calculate the sum of the largest subarrays */
for(i=1;i<m;i++)
{
if (current < 0)current = 0;
current += arr[i];
if(current>max)
{
max = current;
end=i;// The end subscript of the largest subarray
}
}
int temp=max;
/* Calculate the end subscript of the largest subarray */
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(" Enter the number :");
scanf("%d", &n);
int *arr;
arr = (int*)malloc(n * sizeof(int));
printf(" Input %d It's an integer :",n);
for (int i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
solution(n, arr);
return 0;
}
Running results :
边栏推荐
- VPP cannot load up status interface
- [cloud co creation] design Huawei cloud storage architecture with the youngest cloud service hcie (Part 1)
- Zhou Chen, vice president of zhanrui market, responded to everything about 5g chip chunteng 510!
- STL notes (I): knowledge system
- Dragon Dragon community released the first Anolis OS Security Guide to escort users' business systems
- 如何判断是否遭到DDOS攻击
- Performance Optimization: how to solve the slow loading speed of the first screen of spa single page application?
- STM32 Development Notes 118: using CMSIS DSP Library in stm32cube IDE
- LCP plug-in creates peer-to-peer 802.1ad interface
- Go language function
猜你喜欢

自己实现is_class

Teach you three ways to optimize the performance from 20s to 500ms

Preliminary understanding of Panda3D particle system

微服务 - 配置中心 - Nacos

JWT(json web token)

Realsense D435i 深度图优化_高精度模式

C编程 --“最大子数组的和” 的动态规划的解法

epoll的实现原理
[email protected]研发效能度量指标"/>学习记录[email protected]研发效能度量指标

Forwarding and sharing function of wechat applet
随机推荐
Go language function
Oracle split branches
Redis的三个必知必会的问题
Introduction to kubernetes
Sword finger offer special shock edition day 9
Performance Optimization: how to solve the slow loading speed of the first screen of spa single page application?
2022-07-24:以下go语言代码输出什么?A:[]int{};B:[]int(nil);C:panic;D:编译错误。 package main import ( “fmt“ ) f
The third question of force deduction -- the longest substring without repeated characters
服务器防护的七个建议
LCP plug-in creates peer VLAN interface
Add click event to unity 3D object
VPP不能加载UP状态接口
VPP cannot load up status interface
What is virtual DOM? How to implement a virtual DOM
C编程 --“最大子数组的和” 的动态规划的解法
STM32 Development Notes 118: using CMSIS DSP Library in stm32cube IDE
STM32 development note 117: generate IIR low-pass filter coefficients using MATLAB
微服务 - 网关Gateway组件
Leetcode 15: sum of three numbers
Implement is by yourself_ convertible