【6】和作为连续序列s
称号:输入一个整数s,并打印出所有s整数的连续序列(含有至少2的数量)。
如输入9,输出2、3、4和4、5两个序列
方案一:因为序列至少要2个数,则两个数上限值为(1+s)/2,我们能够枚举该序列的起点和终点求全部满足的序列。时间复杂度为O(n^2),效率比較低
方案二:我们设置两个指针start和end分别表示当前序列的起点和终点,并记序列和为sum。
当sum = s的时候输出这个序列,而且end往后移动一位;假设sum > s,则start往后移动一位。假设sum < s,则end要往后移动一位。
直到start == (1+s)/2结束循环,时间复杂度O(n)。效率非常高
- #include<cstdio>
- #include<cstring>
- #include<iostream>
- #include<algorithm>
- using namespace std;
- //打印序列
- void PrintNum(int start, int end){
- for(int i = start; i <= end; i++){
- printf("%d ", i);
- }
- printf("\n");
- }
- //找到两个数和为s
- void FindSequenceSum(int s){
- if(s < 3){ //和小于3是不合法的数据
- return;
- }
- int start = 1;
- int end = 2;
- int sum = 3;
- int mid = (1+s)>>1;
- //循环找到全部的序列
- while(start < mid){ //序列的起点要小于(1+s)的一半
- if(sum == s){ //和为sum的序列直接打印
- PrintNum(start, end);
- end++;
- sum += end;
- }
- else if(sum > s){ //和大于s的序列则起始点往后移动一个
- sum -= start;
- start++;
- }
- else{ //和小于s的序列则终点往后移动一位
- end++;
- sum += end;
- }
- }
- }
- int main(){
- FindSequenceSum(9);
- getchar();
- return 0;
- }
版权声明:本文博主原创文章。博客,未经同意不得转载。
【6】和作为连续序列s的更多相关文章
- [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
- [LeetCode] Longest Consecutive Sequence 求最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- lintcode 最长上升连续子序列 II(二维最长上升连续序列)
题目链接:http://www.lintcode.com/zh-cn/problem/longest-increasing-continuous-subsequence-ii/ 最长上升连续子序列 I ...
- lintcode: 最长连续序列
最长连续序列 给定一个未排序的整数数组,找出最长连续序列的长度. 说明 要求你的算法复杂度为O(n) 样例 给出数组[100, 4, 200, 1, 3, 2],这个最长的连续序列是 [1, 2, 3 ...
- leetcode 最长连续序列 longest consecutive sequence
转载请注明来自souldak,微博:@evagle 题目(来自leetcode): 给你一个n个数的乱序序列,O(N)找出其中最长的连续序列的长度. 例如给你[100, 4, 200, 1, 3, 2 ...
- [LeetCode] Binary Tree Longest Consecutive Sequence II 二叉树最长连续序列之二
Given a binary tree, you need to find the length of Longest Consecutive Path in Binary Tree. Especia ...
- 输入一个正数n,输出所有和为n连续正数序列。例如输入15,由于1+2+3+4+5=4+5+6=7+8=15,所以输出3个连续序列1-5、4-6和7-8。
输入一个正数n,输出所有和为n连续正数序列.例如输入15,由于1+2+3+4+5=4+5+6=7+8=15,所以输出3个连续序列1-5.4-6和7-8. #define N 15 void findS ...
- [Swift]LeetCode128. 最长连续序列 | Longest Consecutive Sequence
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Y ...
- [Swift]LeetCode298. 二叉树最长连续序列 $ Binary Tree Longest Consecutive Sequence
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
随机推荐
- sort如何按指定的列排序·百家电脑学院
sort如何按指定的列排序·百家电脑学院 sort如何按指定的(9php.com)列排序 0000 27189 41925425065f ...
- Fragment总结
一.总体工程图: 二.main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android& ...
- jconsole 连接 eclipse启动项目
eclipse 启动java项目默认没有开启jmx远程查看功能,假设须要看项目执行的线程内存使用量等信息,能够在eclipse启动參数中添加: -Dcom.sun.management.jmxremo ...
- vim经常使用命令总结
vim 选择文本,删除,复制,粘贴 文本的选择,对于编辑器来说,是非常主要的东西,也常常被用到,总结例如以下: v 从光标当前位置開始,光标所经过的地方会被选中,再按一下v结束. V ...
- git merge,rebase和*(no branch)
上一篇:http://blog.csdn.net/xiaoputao0903/article/details/23933589,说了git的分支,相关的使用方法没说到可是仅仅要google就能搜出一大 ...
- hdu1330(递推)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1330 分析:经典问题,n 块相同的木板重叠,最多能够伸出桌面多远 对于n张卡片的最佳摆法,我们只需要在 ...
- C++ Preprosessor import
#import Attributes Provides links to attributes used with the #import directive. Microsoft Specific ...
- 升级旧Delphi应用转向支持手机的一个思路
系统架构改为B/S. 业务规则所有在服务端实现,使用REST服务封装旧有系统,这样可最大程度的利用原有代码. client所实用HTML5+javascript,这样client不须布署PC,可极大减 ...
- Beginning Python From Novice to Professional (4) - 演示样本格式字符串
$ gedit price.py #!/usr/bin/env python width = input('Please enter width: ') price_width = 10 item_w ...
- ecshop 后台添加 成本价 利润
ecshop后台admin中的商品操作php文件,goods.php替换为下面的代码, 还要在数据库商品本店售价后门添加 cost 字段 为 商品成本价 ecs_goods表中添加 cost ...