当前位置: 首页 > news >正文

最大子列和第三种方法有感

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

回到了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函数,就是最好还是写一下,能不能执行是另外一方面,但是规范是在那里的。

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

http://www.gsyq.cn/news/74291.html

相关文章:

  • Trae 实践:从原型图到可执行 HTML 的 AI 编程实现 - 指南
  • 2025年度不锈钢凸轮式转子泵权威排名:铸铁凸轮式转子泵哪家
  • 2025年12月高低温试验箱,恒温恒湿试验箱厂家最新推荐:设备运行噪音测评与品牌介绍
  • 大兴区农村自建房找谁好?北京市大兴区自建房公司/机构深度评测口碑推荐榜。
  • 2025年北京现代化办公家具十大靠谱品牌推荐:含现代化办公茶
  • 2025年12月恒温恒湿试验箱,高低温试验箱厂家最新推荐:科研实验适配性与选择指南
  • 天津短视频拍摄出品厂哪家合作案例多?哪家服务性价比高?
  • 2025年度泵业设备品牌排行榜:拉法泵业市场口碑怎样?
  • 家装公司怎么选才能不踩坑?2025年最新市场洞察与系统化避坑指南
  • 2025年靠谱的316l金属波纹管厂家最新TOP排行榜
  • AI 全域营销天花板!这家浙江企业,帮上千家公司打通国内外增长快车道
  • 2025年逆流闭式冷却塔五大靠谱供应商推荐,来图定制与制造企
  • 2025年热门的云南泡沫包装箱/昆明泡沫包装箱厂家选购指南与推荐
  • 2025年12月关键词厂家推荐:行业权威盘点与品质红榜发布
  • 2025逆流闭式冷却塔制造商TOP5权威推荐:甄选优质工厂与
  • json字符数
  • 2025年口碑好的高纤维狗粮高端品质推荐榜
  • 2025年质量好的挂架厨房收纳专业推荐榜单
  • 2025 年 12 月立体车库厂家权威推荐榜:智能升降/循环式/机械停车库,空间革新与高效存取解决方案深度解析
  • 家装到底该选哪家?2025年最新市场洞察与一家国企背景综合服务商的深度案例推荐
  • 北京知名律所推荐 2026:整体大排名 TOP5,高性价比机构名单
  • 随钻测井中的高温定向传感:原理、进展与行业影响
  • NOIP 总结
  • 2025年评价高的异型管缩管机。热门厂家推荐榜单
  • 2025年度哈尔滨新能源汽车升级服务企业推荐:比较不错的新能
  • 2026 北京擅长打离婚官司的律所排名:5 家靠谱机构解析,高胜诉率诉讼方案推荐
  • 2025年热门的食品级转子泵厂家最新实力排行
  • 2026年河北石家庄赵县农村自建房推荐榜,图南建房宝领衔 六家实力公司赋能乡村宜居生活
  • 2025年质量好的商业动画制作/工业产品动画制作高评分推荐榜单
  • 2026年河北衡水景县农村自建房推荐榜,图南建房宝领衔 六家实力公司赋能乡村宜居生活