【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 ...
- 【6】和作为连续序列s
称号:输入一个整数s,并打印出所有s整数的连续序列(含有至少2的数量). 如输入9,输出2.3.4和4.5两个序列 方案一:因为序列至少要2个数,则两个数上限值为(1+s)/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 ...
随机推荐
- Drupal配置文件settings.php搜索规则
Drupal的配置文件搜索是通过bootstrap.inc的conf_path()函数实现的: function conf_path($require_settings = TRUE, $reset ...
- OTG_FS_ID功能及引申
1. 概要 OTG设备使用插头中的ID引脚来区分A/B Device,ID接地被称作为A-Device,充当USB Host,A-Device始终为总线 提供电力,ID悬空被称作为B-Devi ...
- VMware 命令行下安装以及导入Ubuntu系统
前提: 鉴于个人PC性能太弱,考虑是否可以将在PC上搭建好的环境移植到高性能服务器上.想到后就干呗. 下载完对应操作系统的安装包后按如下步骤操作: 安装包名称:VMware-Workstation-F ...
- HTTP Post请求过程详解
摘要: HTTP(HyperText Transfer Protocol),超文本传输协议,是一个客户端和服务器端请求和应答的标准(TCP),客户端是终端用户,服务器端是网站. HTTP是基于Sock ...
- Scrum角色
产品负责人(Product Owner)的职责如下: 确定产品的功能. 决定发布的日期和发布内容. 为产品的profitability of the product (ROI)负责. 根据 ...
- Tcl之group arguments
1 doubel quotes This allows substitutions to occur within the quotations - or "interpolation&qu ...
- Windows Azure 虚拟网络配置(Point to Site)
说明:本文以Azure国际版为例,中国版在网络位置会存在一定差异. 1. 场景 虚拟网络为我们提供了在Windows Azure云计算环境上构建网络定义的能力,通过虚拟网络,我们可以方便地将Windo ...
- python异常以及面向对象编程
一.面向对象 需要注意的是,在Python中,变量名类似__xxx__的,也就是以双下划线开头,并且以双下划线结尾的,是特殊变量,特殊变量是可以直接访问的,不是private变量,所以,不能用__na ...
- NServiceBus教程-消息传递与处理
nservicebus"的容错默认"设计的一部分,基础设施管理事务自动所以你不需要记住所有的线程和状态管理要素配置. 客户端和服务器 理想情况下,服务器代码处理消息事务,但它往往不 ...
- schedule和scheduleUpdate
在init()函数里面加上scheduleUpdate(),这样才会每一帧都调用update(). Schedule() 函数有两种方式,一种带时间参数,一种不带时间参数. 带时间参数的,间隔指定时间 ...