当前位置:网站首页>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 :
边栏推荐
- flex布局常用属性总结
- Forwarding and sharing function of wechat applet
- 项目管理工具——阿里云Projex介绍与实战
- 2022-07-24: what is the output of the following go language code? A:[]int{}; B:[]int(nil); C:panic; D: Compilation error. package main import ( “fmt“ ) f
- What about reinstalling win11 system?
- Which side of Nacos has the SQL script of this column?
- 龙蜥社区发布首个 Anolis OS 安全指南 为用户业务系统保驾护航
- VPP不能加载UP状态接口
- SystemVerilog中$write与$display区别
- Implement is by yourself_ convertible
猜你喜欢
![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

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

JWT(json web token)

Dragon Dragon community released the first Anolis OS Security Guide to escort users' business systems

rhcsa暑假第三天

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

自己实现is_class

1310_ Implementation analysis of a printf

微服务 - Hystrix 熔断器

基于云原生的私有化 PaaS 平台交付实践
随机推荐
Thesis reading | which is the best multilingual pre training technology for machine translation? See the latest progress!
Openfegin remote call lost request header problem
Guanghetong and Intel released the global version of 5g communication module
STM32 development note 117: generate IIR low-pass filter coefficients using MATLAB
STL notes (VII): container deque
background
Performance Optimization: lazy loading of pictures
Tips for downloading videos in batches
微服务 - 远程调用(Feign组件)
The third day of rhcsa summer vacation
Sword finger offer special shock edition day 9
Vim查找替换及正则表达式的使用
Project management tools - project developer tools
Powering 5g notebook market, Wentai technology joined the "Honghu plan"
AirServer 7.3.0中文版手机设备无线传送电脑屏幕工具
What about reinstalling win11 system?
How to judge whether it is attacked by DDoS
systemverilog中function和task区别
单点登录(一处登录,处处可用)
Three billion dollars! Horizon becomes the world's highest valued AI chip Unicorn