PTA 程序设计题(数据结构第一章)

Posted xmdykf

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PTA 程序设计题(数据结构第一章)相关的知识,希望对你有一定的参考价值。

C语言版

第一题 二分查找

感觉还好

Position BinarySearch(List L, ElementType X)
{
    // 数组大小
    // int N = sizeof(L->Data) / sizeof(*L->Data);
    int start = 1;
    int end = L->Last;
    int mid;
    while (start <= end)
    {
        mid = (start + end) / 2;
        if (L->Data[mid] > X)
            end = mid - 1;
        else if (L->Data[mid] < X)
            start = mid + 1;
        else
            return mid;
    }
    return NotFound;
}

第二题 最大子序列和

方法1

#include<stdio.h>
#include<malloc.h>
int Sum(int A[], int N)
{
 int maxSum, thisSum;
 maxSum = thisSum = 0;
 for (int i = 0; i < N; i++)
 {
 thisSum = 0;
 for (int j = i; j < N; j++)
 {
 for (int k = i; k <= j; k++)
 thisSum += A[k];
 if (thisSum > maxSum)
 maxSum = thisSum;
 }
 }
 return maxSum;
}
int main(void)
{
 int* a = NULL;
 int N;
 scanf("%d", &N);
 a = (int*)malloc(N * sizeof(int));
 for (int i = 0; i < N; i++)
 {
 scanf("%d", &a[i]);
 }
 printf("%d
", Sum(a, N));
}

方法二

#include<stdio.h>
#include<malloc.h>
int Sum(int A[], int N)
{
 int maxSum, thisSum;
 maxSum = thisSum = 0;
 for (int i = 0; i < N; i++)
 {
 thisSum += A[i];
 if (thisSum > maxSum)
 {
 maxSum = thisSum;
 }
 else if (thisSum < 0)
 {
 thisSum = 0;
 }
 }
 return maxSum;
}
int main(void)
{
 int* a = NULL;
 int N;
 scanf("%d", &N);
 a = (int*)malloc(N * sizeof(int));
 for (int i = 0; i < N; i++)
 {
 scanf("%d", &a[i]);
 }
 printf("%d
", Sum(a, N));
}

方法四 在线处理

#include<stdio.h>
#include<malloc.h>
#include<time.h>
clock_t start, endness;
double duration;
#define recyle_k 1e7
int Sum(int A[], int N, int* front, int* end)
{
 int maxSum, thisSum;
 maxSum = thisSum = 0;
 for (int i = 0; i < N; i++)
 {
 thisSum += A[i];
 if (thisSum > maxSum)
 {
 maxSum = thisSum;
 *end = i;
 }
 else if (thisSum < 0)
 {
 thisSum = 0;
 *front = i + 1;
 }
 }
 return maxSum;
}
int main(void)
{
 int* a = NULL;
 int N;
 int front, end;
 int result;
 scanf_s("%d", &N);
 front = 0, end = N;
 a = (int*)malloc(N * sizeof(int));
 for (int i = 0; i < N; i++)
 {
 scanf_s("%d", &a[i]);
 }
 start = clock();
 for (int i = 0; i < recyle_k; i++)
 {
 result = Sum(a, N, &front, &end);
 }
 endness = clock();
 duration = ((double)(endness - start)) / CLK_TCK / recyle_k;
 printf("消耗时间: %2.6e 
", duration);
 printf("最大和: %d
", result);
 printf("子序列首尾:[%d, %d]", front, end);
 return 0;
}

关于 返回子序列首尾,不知道为什么总是不对,吐血

#include<stdio.h>
#include<malloc.h>

int Sum(int A[], int N, int* front, int* end)
{
    int maxSum = 0, thisSum = 0;
    int flag = 0, m = 0;  // flag 作为 全负数 标记 + -2 -3 5 0 
    for (int i = 0; i < N; i++)
    {
        thisSum += A[i];
        if (thisSum < 0)
        {
            thisSum = 0;
        }
        else if (thisSum > maxSum)
        {
            maxSum = thisSum;
            *end = i;
        }
        if (A[i] == 0)
        {
            flag = 0;
            m = i;

        }
    }

    maxSum = thisSum > maxSum ? thisSum : maxSum;
    if (maxSum != 0)
    {
        thisSum = 0;
        for (int i = *end; i >= 0 ; i--)
        {
            thisSum += A[i];
            if (maxSum == thisSum)
            {
                *front = i;
            }
        }
    }
    else if (flag)
    {
        *front = 0;
        *end = N;
    }
    else {
        *front = m;
        *end = m;
    }
    return maxSum;
}
int main(void)
{
    int* a = NULL;
    int N;
    int front, end;
    int result;
    scanf("%d", &N);
    front = 0, end = N;
    a = (int*)malloc(N * sizeof(int));
    for (int i = 0; i < N; i++)
    {
        scanf("%d", &a[i]);
    }
    result = Sum(a, N, &front, &end);
    printf("%d %d %d ", result, front, end);
    return 0;
}

暂时先这样吧。纠结

以上是关于PTA 程序设计题(数据结构第一章)的主要内容,如果未能解决你的问题,请参考以下文章

求助,PSP超级弹丸论破2第一章PTA 卡住不动了.是BUG吗?

C Primer plus 第一章复习题及其编程题

PTA乙级 (1049 数列的片段和 (20分))

为啥玩到弹丸论破2第一章PTA会卡?就是说画面还是动的但是上面的时间不动,花村的话也不出来。为啥?

基于案例的软件构造教程第一章课后题答案

现代软件工程 第一章概论习题第1题 李艳薇