sdut-1351-Max Sum-hdu-1003

本文介绍了一种求解整数序列中最大和子序列的算法,并提供了完整的C语言实现代码。通过对输入序列的逐个元素扫描,算法能够高效地找到具有最大和的连续子序列及其起始和终止位置。

题目描述

Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequence. For example, given (6,-1,5,4,-7), the max sum in this sequence is 6 + (-1) + 5 + 4 = 14.

输入

The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line starts with a number N(1<=N<=100000), then N integers followed(all the integers are between -1000 and 1000).

输出

For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line contains three integers, the Max Sum in the sequence, the start position of the sub-sequence, the end position of the sub-sequence. If there are more than one result, output the first one. Output a blank line between two cases.

示例输入

2
5 6 -1 5 4 -7
7 0 6 -1 1 -6 7 -5

示例输出

Case 1:
14 1 4

Case 2:
7 1 6


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int ls[100005];
int begin[100005];
int main()
{
    int t;
    scanf("%d",&t);
     for(int r = 1; r<=t; r++)
    {
        int n;
        scanf("%d",&n);
        int i;
        for(i = 1; i <= n; i++)
            scanf("%d",&ls[i]);
        int max= 0, sum = 0;
        int top = 0, end = 1;
        max = ls[1];
        sum = ls[1];
        begin[++top] = 1;
        for(i = 2; i <= n; i++)
        {
            sum += ls[i];
            if(sum >= max)
            {
                end = i;
                max = sum;
            }
            else if(sum < 0)
            {
                sum = 0;
                begin[++top] = i+1;
            }
        }
        printf("Case %d:\n",r);
        while(begin[top]>end)
        {
            top--;
        }
        if(r < t)
        {
            printf("%d %d %d\n",max,begin[top],end);
            printf("\n");
        }
        else
        {
            printf("%d %d %d\n",max,begin[top],end);
        }
    }
    return 0;
}


你提到的“7-5 sdut-C语言实验- 排序”应该是山东科技大学(SDUT)在线判题系统中的一道C语言编程实验题,题目要求对一组整数进行排序。下面我将为你详细解答该类问题的常见实现方式、注意事项以及完整可运行的C语言代码。 --- **问题描述(典型情况):** 输入n个整数,将其从小到大排序后输出。 **输入格式:** 第一行输入一个整数n(1 ≤ n ≤ 1000),表示整数个数。 第二行输入n个整数,空格分隔。 **输出格式:** 输出排序后的n个整数,空格分隔,末尾无空格。 --- ### 解决方法: 我们可以使用以下几种方式实现排序: 1. **冒泡排序(适合初学者理解)** 2. **快速排序(效率高,适合数据量较大)** 3. **调用 `qsort` 函数(标准库函数,推荐用于竞赛或实验)** 下面提供一种**使用 `qsort` 的高效解法**,这是在OJ系统中常用且简洁的方法。 ```c #include <stdio.h> #include <stdlib.h> // 比较函数,用于qsort int compare(const void *a, const void *b) { return (*(int*)a - *(int*)b); } int main() { int n; scanf("%d", &n); int arr[1000]; // 题目通常限制n <= 1000 for (int i = 0; i < n; i++) { scanf("%d", &arr[i]); } // 使用qsort排序 qsort(arr, n, sizeof(int), compare); // 输出结果,注意末尾无空格 for (int i = 0; i < n; i++) { if (i > 0) printf(" "); printf("%d", arr[i]); } printf("\n"); return 0; } ``` --- ### 解释: 1. **`qsort` 函数说明:** - 原型:`void qsort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *))` - `base`:数组首地址 - `nmemb`:元素个数 - `size`:每个元素大小(如 `sizeof(int)`) - `compar`:比较函数,升序返回负数/零/正数 2. **比较函数逻辑:** - `return (*(int*)a - *(int*)b);` 实现升序。 - 若改为 `*(int*)b - *(int*)a` 则为降序。 3. **输出控制:** - 使用 `if(i>0) printf(" ")` 避免末尾多余空格,符合OJ格式要求。 4. **数组大小:** - 定义为 `int arr[1000]` 足够应对题目限制。 --- ### 注意事项: - 不要忘记包含 `<stdlib.h>`,否则 `qsort` 会报错。 - 若使用冒泡排序等手动方式,时间复杂度为 O(n²),在 n 较大时可能超时。 - 输入输出使用 `scanf` 和 `printf` 提高效率。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值