1003 郁闷了一下午的题

看题不仔细,没考虑所有数字都可能小于0的情况,都被DS书上的例子给影响了。

 

#include <stdio.h>
#include 
<string.h>
#include 
<stdlib.h>

int res[110000];
int main()
{
    
int cnt, n;
    
int thisSum, maxSum;
    
int i, j;
    
int tag;
    
int max;
    
int sta, msta, lst, mlst;

    scanf(
"%d"&cnt);
    
for (i = 1; i <= cnt; ++i) {
        printf(
"Case %d:\n", i);
        scanf(
"%d"&n);
        tag 
= 1;
        
for (j = 1; j <= n; ++j) {
            scanf(
"%d"&res[j]);
            
if (res[j] >= 0) tag = 0;
        }

        
if (tag) {
            max 
= 1;
            
for (j = 2; j <= n; ++j) {
                
if (res[j] > res[max]) max = j;
            }

            printf(
"%d %d %d\n", res[max], max, max);
        }

        
else {
            
for (j = 1; j <= n; ++j) {
                
if (res[j] >= 0break;
            }

            thisSum 
= maxSum = res[j];
            sta 
= lst = j;
            msta 
= mlst = j;
            
for (j = j+1; j <= n; ++j) {
                thisSum 
+= res[j];
                lst 
= j;
                
if (thisSum > maxSum) {
                    maxSum 
= thisSum;
                    msta 
= sta; mlst = lst;
                }
 else if (thisSum < 0{
                    thisSum 
= 0;
                    sta 
= lst = j+1;
                }

            }

            printf(
"%d %d %d\n", maxSum, msta, mlst);
        }

        
if (i != cnt) printf("\n");
    }

    system(
"pause");
    
return 0;
}

 

posted @ 2009-04-14 20:21  断桥残雪  阅读(91)  评论(0)    收藏  举报