百度之星程序设计大赛(1)——连续正整数(算法2)
来源:互联网 发布:二战瑞典知乎 编辑:程序博客网 时间:2024/06/08 16:29
本博客(http://blog.csdn.net/livelylittlefish)贴出作者(三二一、小鱼)相关研究、学习内容所做的笔记,欢迎广大朋友指正!
自然数写为连续正整数之和
一个正整数有可能可以被表示为n(n>=2)个连续正整数之和,如:
15=1+2+3+4+5
15=4+5+6
15=7+8
15=1+2+3+4+5
15=4+5+6
15=7+8
请编写程序,根据输入的任何一个正整数,找出符合这种要求的所有连续正整数序列。
输入数据:一个正整数,以命令行参数的形式提供给程序。
输出数据:在标准输出上打印出符合题目描述的全部正整数序列,每行一个序列,每个序列都从该序列的最小正整数开始、以从小到大的顺序打印。如果结果有多个序列,按各序列的最小正整数的大小从小到大打印各序列。此外,序列不允许重复,序列内的整数用一个空格分隔。如果没有符合要求的序列,输出“NONE”。
例如,对于15,其输出结果是:
15=1+2+3+4+5
15=4+5+6
15=7+8
对于16,其输出结果是:
NONE
算法思想:
设x1+x2+...+xi=n, 条件: i>=2,x1<=n/2
其中:
其中:
x2=x1+1
x3=x1+2
。。。
xi=x1+i-1
i*x1+i*(i-1)/2=n => x1=(n-i*(i-1)/2 )/i=n/i-(i-1)/2
代码如下:
/**//************************************************************************
* 一个正整数有可能可以被表示为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);
};
//求自然数的连续正整数加法序列
//x1+x2+...+xi=n, 条件: i>=2,x1<=n/2
//x2=x1+1
//x3=x1+2
//xi=x1+i-1
//i*x1+i*(i-1)/2=n => x1=(n-i*(i-1)/2 )/i=n/i-(i-1)/2
void PositiveInteger::GetIntegerSequence(int n)
...{
int i,sum,begin;
i=2;
while(1)
...{
begin=n/i-(i-1)/2; //计算开始值
if(begin<1)
break;
//从begin开始的连续i个正整数和是否为n
sum=i*begin+i*(i-1)/2;
if(sum==n) //相同,则是我们所要求的,否则继续求解
...{
m_vecBegin.push_back(begin);
m_vecEnd.push_back(begin+i-1);
}
i++;
}
}
void PositiveInteger::display(int n)
...{
int size=m_vecBegin.size();
if(size==0)
...{
printf(" NONE ");
}
else
...{
for(int i=size-1;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)个连续正整数之和,如:
* 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);
};
//求自然数的连续正整数加法序列
//x1+x2+...+xi=n, 条件: i>=2,x1<=n/2
//x2=x1+1
//x3=x1+2
//xi=x1+i-1
//i*x1+i*(i-1)/2=n => x1=(n-i*(i-1)/2 )/i=n/i-(i-1)/2
void PositiveInteger::GetIntegerSequence(int n)
...{
int i,sum,begin;
i=2;
while(1)
...{
begin=n/i-(i-1)/2; //计算开始值
if(begin<1)
break;
//从begin开始的连续i个正整数和是否为n
sum=i*begin+i*(i-1)/2;
if(sum==n) //相同,则是我们所要求的,否则继续求解
...{
m_vecBegin.push_back(begin);
m_vecEnd.push_back(begin+i-1);
}
i++;
}
}
void PositiveInteger::display(int n)
...{
int size=m_vecBegin.size();
if(size==0)
...{
printf(" NONE ");
}
else
...{
for(int i=size-1;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);
}
}
运行结果如下:
- 百度之星程序设计大赛(1)——连续正整数(算法2)
- 百度之星程序设计大赛(1)——连续正整数
- 2005年百度之星程序大赛初赛题目(连续正整数)
- 转(百度之星程序设计大赛)
- 百度之星程序设计大赛题目(2)
- hdu 5247 找连续数-2015年百度之星程序设计大赛 - 初赛(1)
- 2015年百度之星程序设计大赛——资格赛(1002列变位法解密)
- 百度之星程序设计大赛试题 -2
- 百度之星程序设计大赛试题(第一题解答)
- “百度之星”程序设计大赛试题解答(Java实现)
- 2014年百度之星程序设计大赛 - 资格赛 (未完成)
- 百度之星程序设计大赛
- 2007年百度之星程序设计大赛初赛题目——百度的高级搜索方法
- 2005年百度之星初赛题目第一题(连续正整数)
- 百度之星astar2008程序设计大赛预赛2
- 2005年百度之星程序设计大赛初赛题目(2)
- 2015年百度之星程序设计大赛 - 初赛(2)【题解】
- 2005年百度之星程序设计大赛初赛题目(1)
- SQL 2008 T-Prep 之旅
- ASP.NET 提供的CaLLBack回调技术实现页面无刷新
- jsf中dataTable嵌套dataTable的实例
- pSOSystem系统结构及模块
- 震撼:Google, Microsoft, Yahoo Maps
- 百度之星程序设计大赛(1)——连续正整数(算法2)
- AT&T汇编语言的相关知识
- 内核模式的dll编写与调用
- VC中写打印程序遇到关于Print Spooler的小问题
- fsd hook里获取文件全路径
- MB Probe
- 高手求助 关于搭建服务器方面的问题
- 两会后的点点感受
- 选择好的idc网站空间--虚拟主机--空间速度测试方法