标签:style csharp ++ 连续子序列 sharp 子序列和 for div span
方法一
//求解最大连续子序列和问题 #include<stdio.h> long maxSubSum(int a[],int n){ int i,j,k; long maxSum=a[0],thisSum; for(i=0;i<n;i++){ for(j=i;j<n;j++){ thisSum=0; for(k=0;k<j;k++) thisSum += a[k]; if(thisSum > maxSum) maxSum = thisSum; } } } int main(){ int a[] = {-2,11,-4,13,-5,-2},n=6; printf("a序列的最大连续子序列的和:%d",maxSubSum(a,n)); }
方法二
#include<stdio.h> int maxSubSum(int a[],int n){ int i,j; long maxSum=a[0],thisSum; for(i=0;i<n;i++){ thisSum=0; for(j=i;j<n;j++){ thisSum += a[j]; if(thisSum > maxSum) maxSum = thisSum; } } } int main(){ int a[] = {-2,11,-4,13,-5,-2},n=6; printf("a序列的最大连续子序列的和:%d",maxSubSum(a,n)); }
方法三
#include<stdio.h> int maxSubSum(int a[],int n){ int i,maxSum=0,thisSum=0; for(i=0;i<n;i++){ thisSum += a[i]; if(thisSum < 0) thisSum = 0; if(maxSum < thisSum) maxSum = thisSum; } } int main(){ int a[] = {-2,11,-4,13,-5,-2},n=6; printf("a序列的最大连续子序列的和:%d",maxSubSum(a,n)); }
#include<stdio.h>int maxSubSum(int a[],int n){int i,j;long maxSum=a[0],thisSum;for(i=0;i<n;i++){thisSum=0;for(j=i;j<n;j++){thisSum += a[j];if(thisSum > maxSum)maxSum = thisSum;}}}int main(){int a[] = {-2,11,-4,13,-5,-2},n=6;printf("a序列的最大连续子序列的和:%d",maxSubSum(a,n));}
标签:style csharp ++ 连续子序列 sharp 子序列和 for div span
原文地址:https://www.cnblogs.com/Hqx-curiosity/p/12146743.html