PTA A1007&A1008

Posted z-y-k

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PTA A1007&A1008相关的知识,希望对你有一定的参考价值。

第四天

A1007 Maximum Subsequence Sum (25 分)

题目内容

Given a sequence of K integers N1, N2, ..., NK . A continuous subsequence is defined to be Ni, Ni+1, ..., Nj
?? 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

单词

continuous

英 /k?n‘t?nj??s/ 美 /k?n‘t?nj??s/
adj. 连续的,持续的;继续的;连绵不断的

indices

英 /‘?nd?si?z/ 美 /‘?nd?siz/

n. 指数;目录(index的复数)

题目分析

最大子列和问题,在MOOC数据结构课程中听姥姥说过一遍,自己也写过,再写的时候发现有点忘了,于是翻了以前的代码。。。。不忍直视啊=-=,复习了一遍姥姥的视频,自己重写了一遍,据说解决方法是动态规划,并没有深入了解。

具体代码

#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 10000
int N;
int a[MAXSIZE];
int begin, maxbegin, maxend;
int maxsum = -1, sum;

int main(void)

    scanf("%d", &N);
    for (int i = 0; i < N; i++)
        scanf("%d", &a[i]);
    for (int i = 0; i < N; i++)
    
        sum += a[i];
        if (sum > maxsum)
        
            maxsum = sum;
            maxbegin = begin;
            maxend = i;
        
        if (sum < 0)
        
            begin = i + 1;
            sum = 0;
        
    
    if (maxsum == -1)
        printf("%d %d %d", sum, a[0], a[N - 1]);
    else
        printf("%d %d %d", maxsum, a[maxbegin], a[maxend]);
    system("pause");

参考博客

【C/C++】Maximum Subsequence Sum/最大子列和问题

A1008 Elevator (20 分)

题目内容

The highest building in our city has only one elevator. A request list is made up with N positive numbers. The numbers denote at which floors the elevator will stop, in specified order. It costs 6 seconds to move the elevator up one floor, and 4 seconds to move down one floor. The elevator will stay for 5 seconds at each stop.
For a given request list, you are to compute the total time spent to fulfill the requests on the list. The elevator is on the 0th floor at the beginning and does not have to return to the ground floor when the requests are fulfilled.

Input Specification:

Each input file contains one test case. Each case contains a positive integer N, followed by N positive numbers. All the numbers in the input are less than 100.

Output Specification:

For each test case, print the total time on a single line.

Sample Input:

3 2 3 1

Sample Output:

41

单词

request

英 /r?‘kwest/ 美 /r?‘kw?st/

n. 请求;需要
vt. 要求,请求

denote

英 /d?‘n??t/ 美 /d?‘not/
vt. 表示,指示

specified

英 /?spes?fa?d/ 美 /?sp?s?fa?d/

v. 指定;详细说明(specify的过去分词)
adj. 规定的;详细说明的

fulfill

英 /ful‘fil/ 美 /ful‘fil/
vt. 履行;实现;满足;使结束(等于fulfil)

题目分析

没什么好说的,小学生加减法而已。

具体代码

#include<stdio.h>
#include<stdlib.h>

int N;
int last;
int time;

int main(void)

    scanf("%d", &N);
    for (int i = 0; i < N; i++)
    
        int n;
        scanf("%d", &n);
        int temp = n;
        n = n - last;
        if (n > 0)
            time += n * 6;
        else if (n < 0)
            time += (-n) * 4;
        time += 5;
        last = temp;
    
    printf("%d", time);
    system("pause");

以上是关于PTA A1007&A1008的主要内容,如果未能解决你的问题,请参考以下文章

一天两道pat1007,1008

[PTA] PAT(A) 1007 Maximum Subsequence Sum (25 分)

PTA(BasicLevel)-1008数组元素循环右移问题

PTA A1001&A1002

PTA A1009&A1010

PTA 乙级 1007 素数对猜想 (20分)