【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 ...
随机推荐
- SLB 权重问题
<pre name="code" class="html">一般配置SLB的时候有个权重0到100,是如何选择数值的? 权重需要您根据后端机器的配置 ...
- perl 访问网站一些useragent的设置
121.40.205.143 - - [22/Jun/2016:12:56:23 +0800] "GET /wechat/account.html HTTP/1.1" 200 34 ...
- 改动file header (測)
--改动file header ------------------------------------------------------------------------- cd $ORACLE ...
- stripslashes和addslashes的区别
我们在向mysql写入数据时,比如: mysql_query(”update table set `title`=’kuhanzhu’s blog’”); 那就会出错.同asp时一样,数据库都会对单引 ...
- Shell简易学习练习
1.Linux Shell入门 Quiz 1 一个接受命令行参数的shell脚本 任务 编写一个shell脚本1.sh,这个脚本接受一个命令行参数,并把这个参数打印两次到标准输出. 如果输入没有参数输 ...
- zzu--2014年11月16日月潭赛 B称号
1229: Rational Resistance Time Limit: 1 Sec Memory Limit: 128 MB Submit: 8 Solved: 4 [id=1229" ...
- jdk1.6与1.7垃圾回收
最近项目中遇到了个关于JVM中GC线程数的问题,做一下简单的总结 问题场景: server:均为 sun公司的solaris 系统 CPU 128个 项目8.1时使用的 java版本: jdk1. ...
- JAVA card 应用开发(三) 把APPLET(CAP文件)装载到卡片
依据前面两篇博文.我们能够在Eclipse上建立好Applet.而且能够有多个AID.能够选择不同的应用. 那么,以上我们都是基于模拟环境的逻辑,实际上有些函数接口是须要实际的环境.就是说我们须要把A ...
- SE 2014年4月22日(一)
实验 练习: 如图配置: 两自治系统 AS 100 和 AS 200 AS 100 是由两私有自治系统 (AS 65001 和 AS 65002)构成 要求配置BGP联盟 使得 R3 R4 R5 下 ...
- Apache的Mesos和Google的Kubernetes 有什么区别?
Apache的Mesos和Google的Kubernetes 有什么区别?本文来自StackOverFlow上的一个问题,主要讨论Mesos和Kubernetes的区别,相信我们很多人也有同意的疑问. ...