亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如下程序,關于C語言調用數組函數該怎么改?

如下程序,關于C語言調用數組函數該怎么改?

C
喵喔喔 2022-04-17 17:10:12
#include<stdio.h>#define MAXLENGTH 1000int main(){ int a[MAXLENGTH];printf("please input the length of the array:\n");unsigned int n;int z;scanf("%d",&n);int i;printf("please input the array!\n");for(i=0;i<n;i++){ scanf("%d",&a[i]);}for (i=0;i<n;i++){printf("%d",a[i]);}z=max_subsequence_sum(a,n);printf("the maximum subsequence sum is %d\n",z );}int max_subsequence_sum(int a[], unsigned int n){int this_sum, max_sum,best_i,best_j,i,j,k;max_sum=0;best_i=best_j= -1;for (i=0;i<n;i++)//locate begin cursor{for(j=i;j<n;j++)//locate end cursor{this_sum=0;for(k=i;k=j;k++)//sum form i to jthis_sum +=a[k];if(this_sum>max_sum){//update max_sum,best_i,best_jbest_i=i;best_j=j;}}}return (max_sum);}
查看完整描述

1 回答

?
慕的地6264312

TA貢獻1817條經驗 獲得超6個贊

#include<stdio.h>
#define MAXLENGTH 1000
int max_subsequence_sum(int a[], unsigned int n)
{
int this_sum, max_sum,best_i,best_j,i,j,k;
max_sum=0;best_i=best_j= -1;
for (i=0;i<n;i++)//locate begin cursor
{
for(j=i;j<n;j++)//locate end cursor
{
this_sum=0;
for(k=i;k<=j;k++)//sum form i to j k=j改為 k<=j才是i到j的和
this_sum +=a[k];
if(this_sum>max_sum)
{
//update max_sum,best_i,best_j
best_i=i;
best_j=j;
max_sum=this_sum;//更新最大值

}

}
}
return (max_sum);
}
int main()
{
int a[MAXLENGTH];
printf("please input the length of the array:\n");
unsigned int n;
int z;
scanf("%d",&n);
int i;
printf("please input the array!\n");
for(i=0;i<n;i++)
{ scanf("%d",&a[i]);
}
for (i=0;i<n;i++)
{
printf("%d",a[i]);
}
z=max_subsequence_sum(a,n);
printf("the maximum subsequence sum is %d\n",z );
}



查看完整回答
反對 回復 2022-04-19
  • 1 回答
  • 0 關注
  • 99 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號