2006年百度astar编程比赛预赛题#1

第一题(共四题100分):连续正整数(10分)

题目描述:
一个正整数有可能可以被表示为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
评分标准:
程序输出结果是否正确。

http://www.micy.cn/blog/index.php/archives/144.html


第一题我的结果:

None.gif #include  < iostream >
None.gif#include 
< math.h >
None.gif
using   namespace  std;
None.gif
None.gif
void  compute( int  n)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
int result[1000];
InBlock.gif    
int count = 0;
InBlock.gif    
int i=0;
InBlock.gif    
int n2 = n * 2;
InBlock.gif    
int temp = 1;
InBlock.gif    
while(n2%2 == 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        n2 
= n2/2;
InBlock.gif        i
++;
InBlock.gif        temp 
= temp * 2;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
if(n2 == 1)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        cout 
<< "none" << endl;
InBlock.gif        
return;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
if (temp < sqrt(n*2))
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        result[
0= temp;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        result[
0= (2*n)/temp;
ExpandedSubBlockEnd.gif    }

InBlock.gif    count
++;
InBlock.gif    
InBlock.gif    
//计算n2分解因数
InBlock.gif
    i = sqrt(n2);
InBlock.gif    
for(;i > 1; i--)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
if (n2%== 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(n2/< sqrt(2*n) && i != n2/i)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                result[count] 
= n2/i;
InBlock.gif                count 
++;
InBlock.gif                result[count] 
= i;
InBlock.gif                count 
++;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockStart.gifContractedSubBlock.gif            
else dot.gif{
InBlock.gif                result[count] 
= i;
InBlock.gif                count 
++;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
//计算机可能的分解数存入result[]中,
InBlock.gif    
//如果需要调整输出次序,可以对result中的前count个元素排序
InBlock.gif
    for(int k = 0; k < count; k++)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
int first = ((2*n)/result[k] + 1 - result[k])/2;
InBlock.gif        
int sum = 0;
InBlock.gif        
for(int t = first; t < first + result[k]; t++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            cout 
<< t << " ";
InBlock.gif            sum 
= sum + t;
ExpandedSubBlockEnd.gif        }

InBlock.gif        cout 
<< "\n\t n=" << result[k] << "\t sum=" << sum << endl;
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
None.gif
int  main()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
int n;
InBlock.gif    cout 
<< "input a interge:";
InBlock.gif    cin 
>> n;
InBlock.gif    compute(n);
InBlock.gif    cout 
<< "done";
InBlock.gif    
while(1);
InBlock.gif    
return 0;
ExpandedBlockEnd.gif}

None.gif
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值