第四天

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&gt
#include&ltstdlib.h&gt
#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]);elseprintf("%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&gt
#include&ltstdlib.h&gtint 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");
}

转载于:https://www.cnblogs.com/z-y-k/p/11528920.html

PTA A1007A1008相关推荐

  1. C语言 之 PTA乙级错误集锦

    1,很大很大的数输入,并各位加和  PTA-1001 #include <stdio.h> #include <math.h> int main(){int sum=0,cou ...

  2. PTA数据结构与算法题目集6-4 6-3 6-8

    PTA数据结构与算法题目集(中文) 6-4 链式表的按序号查找 ElementType FindKth( List L, int K ){int index = 0;while(L){++index; ...

  3. PTA数据结构与算法题目集 6-9 二叉树的遍历

    PTA数据结构与算法题目集(中文) 6-9 二叉树的遍历 void InorderTraversal( BinTree BT ){if(BT==NULL)return;if(BT->Left){ ...

  4. PTA 家庭房产 (图论,暴搜)

    PTA 家庭房产 (图论,暴搜) 题目详情: 给定每个人的家庭成员和其自己名下的房产,请你统计出每个家庭的人口数.人均房产面积及房产套数. 输入格式: 输入第一行给出一个正整数N(≤1000),随后N ...

  5. PTA—输出全排列 (20分) 递归回溯思想

    PTA-输出全排列 (20分) 递归回溯思想 题目要求: 请编写程序输出前n个正整数的全排列(n<10),并通过9个测试用例(即n从1到9)观察n逐步增大时程序的运行时间. 输入格式: 输入给出 ...

  6. PTA 基础编程题目集 6-6 求单链表结点的阶乘和

    PTA 基础编程题目集 6-6 求单链表结点的阶乘和 本题要求实现一个函数,求单链表L结点的阶乘和.这里默认所有结点的值非负,且题目保证结果在int范围内. 函数接口定义: int Factorial ...

  7. PTA 基础编程题目集 7-27 冒泡法排序 C语言

    PTA 基础编程题目集 7-27 冒泡法排序 C语言 将N个整数按从小到大排序的冒泡排序法是这样工作的:从头到尾比较相邻两个元素,如果前面的元素大于其紧随的后面元素,则交换它们.通过一遍扫描,则最后一 ...

  8. PTA 基础编程题目集 7-33 有理数加法 C语言

    PTA 基础编程题目集 7-33 有理数加法 C语言 本题要求编写程序,计算两个有理数的和. 输入格式: 输入在一行中按照a1/b1 a2/b2的格式给出两个分数形式的有理数,其中分子和分母全是整形范 ...

  9. PTA 基础编程题目集 7-24 约分最简分式 C语言

    PTA 基础编程题目集 7-24 约分最简分式 C语言 分数可以表示为分子/分母的形式.编写一个程序,要求用户输入一个分数,然后将其约分为最简分式.最简分式是指分子和分母不具有可以约分的成分了.如6/ ...

最新文章

  1. HDU 2022 海选女主角
  2. 小学五年级就已经开始编程啦吗???
  3. VTK:相互作用之WorldPointPicker
  4. 洛谷P4762: [CERC2014]Virus synthesis(PAM)
  5. list元素求和_LeetCode刷题实战82:删除排序链表中的重复元素 II
  6. 红帽企业 Linux 下载
  7. Qt读取ini文件中文乱码问题
  8. 文本数据标注工具doccano【介绍最详细的一遍文章】
  9. 安卓一个页面设置另一个页面的文本样式_H1标签对于SEO有多重要?页面要不要用H1标签呢?...
  10. 说说Stack Overflow和Quora
  11. vtk体绘制代码报错的解决办法(代码在vtk7,8,9中都能运行),以及VTK数据集网站
  12. 和华明诚:店铺推广要注意哪些事项
  13. 三个等于符号 和两个等于符号的区别
  14. 0x0B-HackTheBox-Obscurity
  15. 搜索进阶之迭代加深搜索
  16. 在家中搭建网站服务器可行吗?
  17. js 延迟几秒执行ifarme_延时加载JavaScript代码提高速度_javascript技巧 -
  18. easypr最新Linux,EasyPR的基本使用
  19. Linux攻击原理,转:Linux下缓冲区溢出攻击的原理及对策
  20. 这些软件测试基础知识你得了解

热门文章

  1. 亚马逊推出FreeTime Android应用程序,开放适合儿童资源
  2. 万能查看电脑连接过的WiFi密码
  3. 2021 ICPC Asia EC网络预选赛I、II
  4. php怎么使用sendcloud,高级功能 - SendCloud 文档中心 - SendCloud 文档中心
  5. 图解汽车各部位的名称大全
  6. 小学生python编程写游戏_用python教小孩子编程做游戏(上)
  7. 手机热点通过蓝牙共享给电脑
  8. 2010年度十大心理学发现
  9. 关于站内信的开发思路
  10. [ECCV2022]3D face reconstruction with dense landmarks