输入一个正数s,打印出所有和为s的连续正数序列
来源:互联网 发布:淘宝君羊令页是真的吗 编辑:程序博客网 时间:2024/04/26 21:17
算法描述:
输入一个正数s,打印出所有和为s的连续正数序列
算法实现:
/*************************************************************************> File Name: main.c> Author: cyf> Mail: 1097189275@qq.com > Created Time: 2016年03月26日 星期六 13时04分31秒 ************************************************************************/#include "FindContinuousSequence.h"/* * 输入一个正数s,打印出所有和为s的连续正数序列(至少包含两个数) * 例如:15->[1-5],[4-6],[7-8] * */int main(){int sum = 15;FindContinuousSequence(sum);return 0;}
/*************************************************************************> File Name: FindContinuousSequence.h> Author: cyf> Mail: 1097189275@qq.com > Created Time: 2016年03月26日 星期六 13时07分35秒 ************************************************************************/#ifndef FINDCONTINUOUSSEQUENCE_H#define FINDCONTINUOUSSEQUENCE_H#include <stdio.h>#include <stdlib.h>void FindContinuousSequence(int sum);void PrintContious(int start, int end);#endif
CC = gccCFLAGS = -g%.o:%.c$(CC) -o $@ -c $(CFLAGS) $<main:main.o FindContinuousSequence.o$(CC) main.o FindContinuousSequence.o -o main $(CFLAGS)clean:rm -rf *.o main
/*************************************************************************> File Name: FindContinuousSequence.c> Author: cyf> Mail: 1097189275@qq.com > Created Time: 2016年03月26日 星期六 13时07分14秒 ************************************************************************/#include "FindContinuousSequence.h"void FindContinuousSequence(int sum){if(sum < 3)return;int small = 1;int big = 2;int middle = (1+sum)>>1;int curSum = small+big;while(small < middle){if(curSum==sum)PrintContious(small, big);while(curSum >sum && small<middle){curSum -= small;small++;if(curSum==sum)PrintContious(small, big);}big++;curSum += big;}}void PrintContious(int start, int end){int i;for(i=start; i<=end; i++)printf("%d ", i);printf("\n");}
0 0
- 输入一个正数s,打印出所有和为s的连续正数序列
- 输入一个正数s,打印出所有和为s的连续正数序列(至少包含两个数)
- 输入一个正数s,打印出所有和为s的连续正数序列(至少包含两个数),例如输入15,打印出连续3个序列1-5,4-6,7-8
- 打印所有和为s的连续正数序列
- Q411:打印所有和为s的连续正数序列
- 剑指Offer——输入正整数S,打印出所有和为S的连续正数序列(至少含有两个正数)
- 剑指off-输入s打印所有和为s的连续正数序列
- 面试题41_2 输入一个正数s,打印所有和为s的连续正数序列(至少两个数)
- 打印和为s的连续正数序列
- 找出所有和为S的连续正数序列
- 输出所有和为S的连续正数序列
- 所有和为S的连续正数序列
- 和为S的连续正数序列
- 和为S的连续正数序列
- 和为S的连续正数序列
- 和为s的连续正数序列
- 和为s的连续正数序列
- 和为s的连续正数序列
- Docker安装
- git pull --rebase丢文件丢失修改了怎么办
- day01
- 如何真正的从细节上节省网站用户的时间
- 一道关于fork的笔试题
- 输入一个正数s,打印出所有和为s的连续正数序列
- Unsupported major.minor version 原理
- Springcache 应用配置说明
- dequeueReusableCellWithIdentifier: 与dequeueReusableCellWithIdentifier: forIndexPath:的区别
- 离散余弦变换matlab
- Android Studio编译输出apk文件修改文件名
- 什么叫共轭先验或者共轭分布?
- MyBatis+Spring 事务管理
- Boosting学习(二)