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&ltstdio.h>
#include&ltstdlib.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&ltstdio.h>
#include&ltstdlib.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");
}

猜你喜欢

转载自www.cnblogs.com/z-y-k/p/11528920.html
PTA