#include<stdio.h> #define MAX_LENGTH 10 int main() { int a[MAX_LENGTH]={1,2,3,-2,4,-6,-8,5,3,1}; int i,j,beg,end,tmp,min=0x7fffffff; //beg和end分别为子数组中首末元素下标,min为无穷大的数 beg=end=tmp=0; for(i=0;i<MAX_LENGTH;++i) { tmp=a[i]; for(j=i+1;j<MAX_LENGTH;++j) { if(a[j]<=0) { tmp+=a[j]; } else break; } if(min>tmp) { beg=i; end=j-1; min=tmp; } } printf("最小值为:%d\n",min); printf("子数组为:"); for(i=beg;i<=end;++i) printf("%d ",a[i]); printf("\n"); return 0; }
时间: 2024-10-12 02:45:40