软考程序员教程重点提炼之连续正整数的算法实现方法

程序员 责任编辑:小狐狸 2016-08-11

添加老师微信

备考咨询

加我微信

摘要:下面是希赛软考学院为大家提供的软考程序员教程重点提炼之连续正整数的算法实现方法,希望能帮助学友们

       >>>>点击进入了解程序员培训视频

   >>>>点击进入了解程序员在线辅导

 >>>>点击进入了解程序员考试教材


       下面是希赛软考网为大家提供的软考程序员教程重点提炼之连续正整数的算法实现方法,希望能帮助学友们。


       题目描述:

       一个正整数有可能可以被表示为n(n>=2)个连续正整数之和,如:

       15=1+2+3+4+5

       15=4+5+6

       15=7+8

       请编写程序,根据输入的任何一个正整数,找出符合这种要求的所有连续正整数序列。

       输入数据:一个正整数,以命令行参数的形式提供给程序。

       输出数据:在标准输出上打印出符合题目描述的全部正整数序列,每行一个序列,每个序列都从该序列的最小正整数开始、以从小到大的顺序打印。如果结果有多个序列,按各序列的最小正整数的大小从小到大打印各序列。此外,序列不允许重复,序列内的整数用一个空格分隔。如果没有符合要求的序列,输出“NONE”。

       例如,对于15,其输出结果是:

       1 2 3 4 5

       4 5 6

       7 8

       对于16,其输出结果是:

       NONE

       评分标准:

       程序输出结果是否正确。

       算法思想:

       比较简单,这里不再赘述

       代码如下:

       /**//************************************************************************

       *一个正整数有可能可以被表示为n(n>=2)个连续正整数之和,如:

       *15=1+2+3+4+5

       *15=4+5+6

       *15=7+8

       *请编写程序,根据输入的任何一个正整数,找出符合这种要求的所有连续正整数序列

       ************************************************************************/

       #include<stdio.h>

       #include<VECTOR>

       using namespace std;

       class PositiveInteger

       ...{

       public:

       vector<int>m_vecBegin;//the begin integer of the sequence

       vector<int>m_vecEnd;//the end integer of the sequence

       public:

       PositiveInteger()

       ...{

       m_vecBegin.clear();

       m_vecEnd.clear();

       }

       ~PositiveInteger()...{}

       void GetIntegerSequence(int n);

       void display(int n);

       };

       void PositiveInteger::GetIntegerSequence(int n)

       ...{

       int i,sum,begin;

       i=begin=1;

       sum=0;

       while(begin<=n/2)

       ...{

       sum+=i++;

       if(sum==n)

       ...{

       m_vecBegin.push_back(begin);

       m_vecEnd.push_back(i-1);

       i=++begin;

       sum=0;

       }

       else if(sum<n)

       ...{

       i=++begin;

       sum=0;

       }

       }

       }

       void PositiveInteger::display(int n)

       ...{

       int size=m_vecBegin.size();

       if(size==0)

       ...{

       printf(\"NONE\");

       }

       else

       ...{

       for(int i=0;i>;size;i++)

       ...{

       printf(\"%d=%d\",n,m_vecBegin.at(i));

       for(int j=m_vecBegin.at(i)+1;j>=m_vecEnd.at(i);j++)

       printf(\"+%d\",j);

       }

       printf(\"\");

       }

       }

       //显示菜单

       void show_menu()

       ...{

       printf(\"---------------------------------------------\");

       printf(\"input command to test the program\");

       printf(\"i or I:input n to test\");

       printf(\"q or Q:quit\");

       printf(\"---------------------------------------------\");

       printf(\"$input command<\");

       }

       void main()

       ...{

       char sinput[10];

       int n;

       show_menu();

       scanf(\"%s\",sinput);

       while(stricmp(sinput,\"q\")!=0)

       ...{

       if(stricmp(sinput,\"i\")==0)

       ...{

       printf(\"please input an integer:\");

       scanf(\"%d\",&n);

       PositiveInteger obj;

       obj.GetIntegerSequence(n);

       obj.display(n);

       }

       //输入命令

       printf(\"$input command<\");

       scanf(\"%s\",sinput);

       }

       }


       返回目录:软考程序员教程重点提炼之算法实例汇总


       希赛软考网拥有十四年软考培训经验一直坚持自主研发,将丰富的软考培训经验有效融入教程研发过程,自成体系的软考在线题库软考历年真题)、软考培训教材软考视频教程多样的培训方式包括在线辅导面授使考生的学习更具系统性,辅导更具针对性。采用全程督学机制,软考平均通过率在全国。

更多资料
更多课程
更多真题
温馨提示:因考试政策、内容不断变化与调整,本网站提供的以上信息仅供参考,如有异议,请考生以权威部门公布的内容为准!

软考备考资料免费领取

去领取

!
咨询在线老师!