虽然工作三年了,但是数据结构还是一塌糊涂,上一份工作就是因为代码写少了所以被开除了,胡玩了三个月之后现在又开始捡原来的知识。

回到了PTA,真怀念上学的时候还在上面疯狂答题的时间。

最大子列和是2025 MOOC杭州大学 数据结构课程问题集的第一道题

第三种方法是陈越老师在慕课里面讲的第三种方法。

言归正传,这次浙江大学的PTA数据结构的习题集第一题最大子列和,编译的过程之中就是报错:ignoring return value of ‘scanf’ declared with attribute ‘warn_unused_result’

就是说如果说键盘输入的数据可能会出现问题

第一点:输入数组长度的时候就是输入了数据。

第二点:以此输入每一个数组的元素

这两个地方报错,所以就是还是有很多的细节需要在下去注意的

修改之前的代码如下:

#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 100000        /* The Max size of the array length */

/**
 * @param A is the one of the three number in the compare
 * @param B is the one of the three number in the compare
 * @param C is the one of the three number in the compare
 */
int Max3(int A, int B, int C)
{
    /* return the biggest number from 3 digit */
    return A > B ? A > C ? A : C : B > C ? B : C;
}

/**
 * @param List is the array List
 * @param left is the left index
 * @param right is the right index
 */
int DivideAndConquer(int List[], int left, int right)
{
    /* divide and conquer method to know List[left] to List[right] 's max subseq sum */
    int MaxLeftSum = 0;            /* The Left Max subseq sum */
    int MaxRightSum = 0;            /* The Right Max subseq sum */
    int MaxLeftBorderSum = 0;        /* The Left Max subseq sum on the border */
    int MaxRightBorderSum = 0;        /* The Right Max subseq sum on the border */

    int LeftBorderSum = 0;            /* The Left Border sum */
    int RightBorderSum = 0;            /* The Right Border sum */

    int center = 0;                /* The border */
    int i = 0;                /* The index */

    if(left == right)
    {            /* the condition to terminate the recursion : left a number */
        if( List[left] > 0)
        {
            return List[left];
        }
        else
        {
            return 0;
        }
    }

    /* The solution to divide */
    center = (left + right) / 2;    /* find the mid center point */
    /* recure to calcuate the max subseq sum */
    MaxLeftSum = DivideAndConquer(List, left, center);
    MaxRightSum = DivideAndConquer(List, center+1, right);

    /* cross the border to calcuate the max subseq sum */
    MaxLeftBorderSum = 0;
    LeftBorderSum = 0;
    for(i = center; i >= left; i--)
    {                /* from center to left scan */
        LeftBorderSum += List[i];
        if(LeftBorderSum > MaxLeftBorderSum)
        {
            MaxLeftBorderSum = LeftBorderSum;
        }
    }                /* left side scan terminated */

    MaxRightBorderSum = 0;
    RightBorderSum = 0;
    for(i = center + 1; i <= right; i++)
    {                /* from center to right scan */
        RightBorderSum += List[i];
        if(RightBorderSum > MaxRightBorderSum)
        {
            MaxRightBorderSum = RightBorderSum;
        }
    }                /* right side scan terminated */

    /* Then we show the conquer result */
    return Max3(MaxLeftSum, MaxRightSum, MaxLeftBorderSum + MaxRightBorderSum);
}

/**
 * @param List is the array to be examine
 * @param N is the length of the array List
 */
int MaxSubseqSum3(int List[], int N)
{
    /* keep the same interface with 2 before algorithm */
    return DivideAndConquer(List, 0, N - 1);
}

int main(int argc, char *argv[])
{
    int length = 0;
    scanf("%d",&length);
    /* Just use malloc function to seek the enought memory space to fill the array */
    int *array = (int*)malloc(length * sizeof(int));
    if(array == NULL)    // Memory malloc failed!
    {
        printf("Memory allocation failed!\n");
    }

    // fixed: The index is i, avoid to overflow
    for(int i = 0; i < length; i++)
    {
        scanf("%d",&array[i]);
    }

    int result = MaxSubseqSum3(array, length);
    printf("%d\n", result);

    /* !!! IMPORTANT OPERATION, free the memory which allocate 
     * Please Just Do it *
     * It is very important */
    free(array);

    return 0;
}

 

编译提示警告:

a.c: In function ‘main’:
a.c:93:5: warning: ignoring return value of ‘scanf’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   93 |     scanf("%d",&length);
      |     ^~~~~~~~~~~~~~~~~~~
a.c:104:13: warning: ignoring return value of ‘scanf’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  104 |             scanf("%d",&array[i]);
      |             ^~~~~~~~~~~~~~~~~~~~~

 

在网络上面就是查询很多的方法,最直接的方法就是检查每一次scanf("%d", &number)的结果是否为1

这是一个在下之前从来都不会想到的,但是从某种意义上面来说,这个也是在下已经忽略了很久的东西。

网络上面有非常多的为了解决而解决这个问题的方法。但是个人并不欣赏这些方法。

还有就是程序注释。很多的程序员不喜欢写注释,但是看别人的代码的时候又不喜欢别人不写注释,这个也是个人需要注意的。

还有就是将各个函数的参数都写清楚名称和作用,向别人描述清楚函数作用

C DocString编码风格和规范,doxygen也是个人之后需要学习的

列一下更改之后的代码:

#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 100000        /* The Max size of the array length */

/**
 * @param A is the one of the three number in the compare
 * @param B is the one of the three number in the compare
 * @param C is the one of the three number in the compare
 */
int Max3(int A, int B, int C)
{
    /* return the biggest number from 3 digit */
    return A > B ? A > C ? A : C : B > C ? B : C;
}

/**
 * @param List is the array List
 * @param left is the left index
 * @param right is the right index
 */
int DivideAndConquer(int List[], int left, int right)
{
    /* divide and conquer method to know List[left] to List[right] 's max subseq sum */
    int MaxLeftSum = 0;            /* The Left Max subseq sum */
    int MaxRightSum = 0;            /* The Right Max subseq sum */
    int MaxLeftBorderSum = 0;        /* The Left Max subseq sum on the border */
    int MaxRightBorderSum = 0;        /* The Right Max subseq sum on the border */

    int LeftBorderSum = 0;            /* The Left Border sum */
    int RightBorderSum = 0;            /* The Right Border sum */

    int center = 0;                /* The border */
    int i = 0;                /* The index */

    if(left == right)
    {            /* the condition to terminate the recursion : left a number */
        if( List[left] > 0)
        {
            return List[left];
        }
        else
        {
            return 0;
        }
    }

    /* The solution to divide */
    center = (left + right) / 2;    /* find the mid center point */
    /* recure to calcuate the max subseq sum */
    MaxLeftSum = DivideAndConquer(List, left, center);
    MaxRightSum = DivideAndConquer(List, center+1, right);

    /* cross the border to calcuate the max subseq sum */
    MaxLeftBorderSum = 0;
    LeftBorderSum = 0;
    for(i = center; i >= left; i--)
    {                /* from center to left scan */
        LeftBorderSum += List[i];
        if(LeftBorderSum > MaxLeftBorderSum)
        {
            MaxLeftBorderSum = LeftBorderSum;
        }
    }                /* left side scan terminated */

    MaxRightBorderSum = 0;
    RightBorderSum = 0;
    for(i = center + 1; i <= right; i++)
    {                /* from center to right scan */
        RightBorderSum += List[i];
        if(RightBorderSum > MaxRightBorderSum)
        {
            MaxRightBorderSum = RightBorderSum;
        }
    }                /* right side scan terminated */

    /* Then we show the conquer result */
    return Max3(MaxLeftSum, MaxRightSum, MaxLeftBorderSum + MaxRightBorderSum);
}

/**
 * @param List is the array to be examine
 * @param N is the length of the array List
 */
int MaxSubseqSum3(int List[], int N)
{
    /* keep the same interface with 2 before algorithm */
    return DivideAndConquer(List, 0, N - 1);
}

int main(int argc, char *argv[])
{
    int length = 0;
    if(scanf("%d",&length) == 1)
    {
    }
    else
    {
        printf("Failed to read integer.\n");
    }
    /* Just use malloc function to seek the enought memory space to fill the array */
    int *array = (int*)malloc(length * sizeof(int));
    if(array == NULL)    // Memory malloc failed!
    {
        printf("Memory allocation failed!\n");
    }

    // fixed: The index is i, avoid to overflow
    for(int i = 0; i < length; i++)
    {
        if(scanf("%d",&array[i]) == 1)
        {
        }
        else
        {
            printf("Failed to read integer.\n");
        }
    }

    int result = MaxSubseqSum3(array, length);
    printf("%d\n", result);

    /* !!! IMPORTANT OPERATION, free the memory which allocate 
     * Please Just Do it *
     * It is very important */
    free(array);

    return 0;
}

还有就是最后一点的Free,老师在上课的时候反复强调过就是malloc和calloc的函数都是需要释放内存的,但是个人在查询了很多资料的时候还是发现有的网站上面并没有写free函数,就是最好还是写一下,能不能执行是另外一方面,但是规范是在那里的。

谢谢老师和同学们不厌其烦的帮助。

posted on 2025-12-06 10:41  RainbowSea26  阅读(0)  评论(0)    收藏  举报