且未

博客园 首页 新随笔 联系 订阅 管理

题目(1231)

#include<stdio.h>
#include<iostream>
using namespace std;
int main()
{
    int K,num[10010],cnt;
    int end,start,thisMax,Max,temp;
    while(cin>>K&&K)
    {
        cnt=0;
        for( int i=0; i<K; i++)
        {
            scanf("%d",&num[i]);
            if(num[i] < 0 )
                cnt++;
        }
        if(cnt == K)
        {
            printf("%d %d %d\n",0,num[0],num[K-1]);
            continue;
        }
        start = end = 0;
        Max = thisMax = num[0];
        for( int i=1; i<K; i++ )
        {
            if(thisMax < 0)
            {
                temp = i;
                thisMax = 0;
            }
            thisMax += num[i];
            if(thisMax > Max)
            {
                Max = thisMax;
                end = i;
                start = temp;
            }
        }
        printf("%d %d %d\n",Max,num[start],num[end]);
    }
    return 0;
}
虽然看了别人的博客AC了但是自己还是不知其所以然。。。


题目(1003)

这道题和上面那道题没有多大的区别,可以说是一模一样,唯一有点疑惑的是,上面那道题的temp 没有赋予初值却对,下面这道题就不可以了
#include<stdio.h>
#include<cstring>
#include<iostream>
using namespace std;
int main()
{
    int T,cnt=0,n;
    int num[100010];
    int thisMax,Max,temp;
    scanf("%d",&T);
    while(T--)
    {
        int start, end;
        memset(num,0,sizeof(num));
        cnt++;
        scanf("%d",&n);
        for(int i=0; i<n; i++)
            scanf("%d",&num[i]);
            start = end = temp = 0;
            thisMax = Max = num[0];

          for( int i=1; i<n; i++ )
            {
                if(thisMax < 0)
                {
                    temp = i;
                    thisMax = 0;
                }
                thisMax += num[i];
                if(thisMax > Max)
                {
                    Max = thisMax;
                    end = i;
                    start = temp;
                }
            }

            if(cnt != 1) printf("\n");
            printf("Case %d:\n",cnt);
            printf("%d %d %d\n",Max,start+1,end+1);

    }
    return 0;
}


posted on 2017-11-30 18:37  阿聊  阅读(102)  评论(0编辑  收藏  举报