题目:输入一个整数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的更多相关文章

  1. [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

  2. [LeetCode] Longest Consecutive Sequence 求最长连续序列

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  3. lintcode 最长上升连续子序列 II(二维最长上升连续序列)

    题目链接:http://www.lintcode.com/zh-cn/problem/longest-increasing-continuous-subsequence-ii/ 最长上升连续子序列 I ...

  4. lintcode: 最长连续序列

    最长连续序列 给定一个未排序的整数数组,找出最长连续序列的长度. 说明 要求你的算法复杂度为O(n) 样例 给出数组[100, 4, 200, 1, 3, 2],这个最长的连续序列是 [1, 2, 3 ...

  5. leetcode 最长连续序列 longest consecutive sequence

    转载请注明来自souldak,微博:@evagle 题目(来自leetcode): 给你一个n个数的乱序序列,O(N)找出其中最长的连续序列的长度. 例如给你[100, 4, 200, 1, 3, 2 ...

  6. 【6】和作为连续序列s

    称号:输入一个整数s,并打印出所有s整数的连续序列(含有至少2的数量). 如输入9,输出2.3.4和4.5两个序列 方案一:因为序列至少要2个数,则两个数上限值为(1+s)/2,我们能够枚举该序列的起 ...

  7. [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 ...

  8. 输入一个正数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 ...

  9. [Swift]LeetCode128. 最长连续序列 | Longest Consecutive Sequence

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Y ...

  10. [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 ...

随机推荐

  1. 嵌入式 hi3518平台指定网卡测试是否通外网

    版权声明:本文为博主原创文章,未经博主允许不得转载. /********************************** (C) COPYRIGHT *********************** ...

  2. Android随笔--使用ViewPager实现简单地图片的左右滑动切换

    Android中图片的左右切换随处可见,今天我也试着查阅资料试着做了一下,挺简单的一个小Demo,却也发现了一些问题,话不多说,上代码~: 使用了3个xml文件作为ViewPager的滑动page,布 ...

  3. [转]Chrome浏览器的离线安装包下载地址

    每当chrome有更新之后,都有不少用户想要下载离线版的安装文件,但苦于找不到下载地址而发愁,其实这个问题很简单,下面我来分享一下方法(仅针对Windows操作系统): 对于稳定版(正式版)Chrom ...

  4. leetcode:Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  5. Python面向对象3

    一.内部类 内部类就是在类的内部定义的类,主要目的是为了更好的抽象现实世界. 二.魔术方法(构造函数和析构函数) #!usr/bin/python #coding:utf8 class Milo(): ...

  6. Python 统计文本中单词的个数

    1.读文件,通过正则匹配 def statisticWord(): line_number = 0 words_dict = {} with open (r'D:\test\test.txt',enc ...

  7. NSRangeFromString 测试

    官网文档 Returns a range from a textual representation. Declaration SWIFT func NSRangeFromString(_ aStri ...

  8. bzoj3714: [PA2014]Kuglarz

    [PA2014]KuglarzTime Limit: 20 Sec Memory Limit: 128 MBSubmit: 553 Solved: 317[Submit][Status][Discus ...

  9. geeksforgeeks@ Minimum Points To Reach Destination (Dynamic Programming)

    http://www.practice.geeksforgeeks.org/problem-page.php?pid=91 Minimum Points To Reach Destination Gi ...

  10. matlab 画平面

    y = :; z = ones(); surf(x,y,z):