• 周四. 12 月 26th, 2024

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

PAT 01-2

admin

11 月 28, 2021
#include<stdio.h> 
#include<stdlib.h>

int main()
{
    int k;
    int *data;
    int i;
    int ThisSum, MaxSum, FirstNum, LastNum, tempFir, tempLas;
    
    while(scanf("%d", &k) != EOF)
    {
        data = (int *)malloc(k * sizeof(int));
        for(i = 0; i < k; i++)
        {
            scanf("%d", &data[i]);
        }
        
        ThisSum = 0;
        MaxSum = 0;
        FirstNum = 0;
        LastNum = k - 1;
        tempFir = 0;
        tempLas = 0;
        
        for(i = 0; i < k; i++)
        {
            if(ThisSum >= 0)
            {
                ThisSum += data[i];
                tempLas = i;
            }
            else
            {
                ThisSum = data[i];
                tempFir = i;
                tempLas = i;
            }
            if(ThisSum > MaxSum  || ((LastNum == k - 1)&&(ThisSum == 0)))
            {
                MaxSum = ThisSum;
                FirstNum = tempFir;
                LastNum = tempLas;
            }
        }
        printf("%d %d %d", MaxSum, data[FirstNum], data[LastNum]);
    }
}

发表回复