題目描述:
一個正整數有可能可以被表示為n(n>=2)個連續(xù)正整數之和,如:
15=1+2+3+4+5
15=4+5+6
15=7+8
請編寫程序,根據輸入的任何一個正整數,找出符合這種要求的所有連續(xù)正整數序列。
輸入數據:一個正整數,以命令行參數的形式提供給程序。
輸出數據:在標準輸出上打印出符合題目描述的全部正整數序列,每行一個序列,每個序列都從該序列的最小正整數開始、以從小到大的順序打印。如果結果有多個序列,按各序列的最小正整數的大小從小到大打印各序列。此外,序列不允許重復,序列內的整數用一個空格分隔。如果沒有符合要求的序列,輸出“NONE”。
例如,對于15,其輸出結果是:
1 2 3 4 5
4 5 6
7 8
對于16,其輸出結果是:
NONE
評分標準:
程序輸出結果是否正確。
算法思想:
比較簡單,這里不再贅述
代碼如下:
/**//************************************************************************
* 一個正整數有可能可以被表示為n(n>=2)個連續(xù)正整數之和,如:
* 15=1+2+3+4+5
* 15=4+5+6
* 15=7+8
* 請編寫程序,根據輸入的任何一個正整數,找出符合這種要求的所有連續(xù)正整數序列
************************************************************************/
#include
#include
using namespace std;
class PositiveInteger
...{
public:
vector m_vecBegin;//the begin integer of the sequence
vector 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 ...{
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);
}
}
一個正整數有可能可以被表示為n(n>=2)個連續(xù)正整數之和,如:
15=1+2+3+4+5
15=4+5+6
15=7+8
請編寫程序,根據輸入的任何一個正整數,找出符合這種要求的所有連續(xù)正整數序列。
輸入數據:一個正整數,以命令行參數的形式提供給程序。
輸出數據:在標準輸出上打印出符合題目描述的全部正整數序列,每行一個序列,每個序列都從該序列的最小正整數開始、以從小到大的順序打印。如果結果有多個序列,按各序列的最小正整數的大小從小到大打印各序列。此外,序列不允許重復,序列內的整數用一個空格分隔。如果沒有符合要求的序列,輸出“NONE”。
例如,對于15,其輸出結果是:
1 2 3 4 5
4 5 6
7 8
對于16,其輸出結果是:
NONE
評分標準:
程序輸出結果是否正確。
算法思想:
比較簡單,這里不再贅述
代碼如下:
/**//************************************************************************
* 一個正整數有可能可以被表示為n(n>=2)個連續(xù)正整數之和,如:
* 15=1+2+3+4+5
* 15=4+5+6
* 15=7+8
* 請編寫程序,根據輸入的任何一個正整數,找出符合這種要求的所有連續(xù)正整數序列
************************************************************************/
#include
#include
using namespace std;
class PositiveInteger
...{
public:
vector
vector
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
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);
}
}

