您当前的位置: 首页 > 

我什么都布吉岛

暂无认证

  • 5浏览

    0关注

    292博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

第一周编程作业: Maximum Subsequence Sum

我什么都布吉岛 发布时间:2018-03-13 14:57:31 ,浏览量:5

01-复杂度2 Maximum Subsequence Sum(25 point(s)) Given a sequence of K integers { N ​1 ​​ , N ​2 ​​ , …, N ​K ​​ }. A continuous subsequence is defined to be { N ​i ​​ , N ​i+1 ​​ , …, N ​j ​​ } where 1≤i≤j≤K. The Maximum Subsequence is the continuous subsequence which has the largest sum of its elements. For example, given sequence { -2, 11, -4, 13, -5, -2 }, its maximum subsequence is { 11, -4, 13 } with the largest sum being 20.

Now you are supposed to find the largest sum, together with the first and the last numbers of the maximum subsequence.

Input Specification: Each input file contains one test case. Each case occupies two lines. The first line contains a positive integer K (≤10000). The second line contains K numbers, separated by a space.

Output Specification: For each test case, output in one line the largest sum, together with the first and the last numbers of the maximum subsequence. The numbers must be separated by one space, but there must be no extra space at the end of a line. In case that the maximum subsequence is not unique, output the one with the smallest indices i and j (as shown by the sample case). If all the K numbers are negative, then its maximum sum is defined to be 0, and you are supposed to output the first and the last numbers of the whole sequence.

Sample Input: 10 -10 1 2 3 4 -5 -23 3 7 -21 Sample Output: 10 1 4

程序实现如下:

#include 
#include 
//void print_arr(int n, int * arr);

int main(void)
{
    int n = 0;
    scanf("%d", &n);
    int * arr = (int *)malloc(n * sizeof(int));

    for (int i = 0; i < n; i++)
    {
        scanf("%d", &arr[i]);
        getchar();
    }

    //int arr[5]={1,2,-8,2,1};
    //n=5;

    //n=10;
    //int arr[10]={10,-2,5,5,-1,-100,100,-2,1,2};
    //int arr[10]={-1,-2,-3,-4,-8,-3,-8,-9,-7,-11};//all the number are negative
    //int arr[10]={-5,-2,8,-1,9,1,-10,0,0,0};//the sum have a negative number
    //int arr[10]={};//all the number is zero
    //int arr[10]={0,0,10,-1,-1,5,-1,-1,-1,-100};
    //int arr[10]={-2,-5,1,50,-1,-1,-2,-300,-4,-8};
    //int arr[10]={100,-8,-8,1,-8,-1000,1,2,0,0};

    //int arr[10]={-1,-2,-3,-5,-1,0,-1,-1,-1,-2};

    int next_sum, current_sum;
    next_sum = current_sum = 0;

    int start=0, end=n-1;//final result
    int start1=0,end1=n-1;//maybe the final result
    int flag=0;
    int tempSum=0;
    int s=0;
    int j=0;
    int k=1;
    int t=0;

   // int flag2 = 0;
    //int start_temp=0;

    for (int i = 0; i < n; i++)//check all the numbers
    {
        current_sum += arr[i];
        if (flag == 0)
        {
            if (arr[i] > 0|| arr[i]==0)//find a posstive number
            {
                start1 = i;//mark it
                t=start1;
                //start=start1;
                //printf("**********%d\n",start1);
                if(arr[i]==0)
                {
                    k++;
                }
                flag = 1;
            }
            else//can't find a suitable number
            {
                current_sum=0;
                //printf("positive!!\n");
                k++;
                continue;

            }
        }
        if ( current_sum < 0)
        {
            s=current_sum;
            tempSum=current_sum-arr[i];//maybe the maxsum
            start1=i+1;
            j=i-1;
            while(arr[j]            
关注
打赏
1658157489
查看更多评论
0.0396s