- 论坛徽章:
- 0
|
根据 #24 楼提议,俩 for :- #include<stdio.h>
- #define N 10
- // 只要结果
- int a[N];
- int max_sum ( int *array, int array_length ) {
- int tags = 0;
- int max, i, j, k, m;
- for (i = 0; i < array_length; i++ ) {
- max = 0;
- for ( j = i; j < array_length; j++ ) {
- max += array[j];
- if ( tags < max || tags == 0 ) {
- k = i;
- m = j;
- tags = max;
- }
- }
- }
- printf ("the max sum is %3d ,from %3d to %3d\n",tags,k+1,m+1 );
- }
- main () {
- int i;
- int *p;
- for (p = a; p < a+ N; p++ )
- scanf ("%d",p);
- p = a;
- max_sum ( p, N );
- }
复制代码- #include<stdio.h>
- #define N 10
- // 显示每次的结果,再作选择
- int a[N];
- int max_sum ( int *array, int array_length ) {
- int max ;
- int tags, i, j, k, m;
- for (i = 0; i < array_length; i++ ) {
- max = tags = 0;
- for ( j = i; j < array_length; j++ ) {
- max += array[j];
- if ( tags < max || tags == 0) {
- k = i;
- m = j;
- tags = max;
- }
- }
- printf ("In %3dth the max sum is %3d ,from %3d to %3d\n",i+1,tags,k+1,m+1 );
- }
- }
- main () {
- int i;
- int *p;
- for (p = a; p < a+ N; p++ )
- scanf ("%d",p);
- p = a;
- max_sum ( p, N );
- }
复制代码 |
|