`
daning
  • 浏览: 89963 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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


第一题我的结果:

#include <iostream>
#include 
<math.h>
using namespace std;

void compute(int n)
{
    
int result[1000];
    
int count = 0;
    
int i=0;
    
int n2 = n * 2;
    
int temp = 1;
    
while(n2%2 == 0)
    
{
        n2 
= n2/2;
        i
++;
        temp 
= temp * 2;
    }

    
if(n2 == 1)
    
{
        cout 
<< "none" << endl;
        
return;
    }

    
if (temp < sqrt(n*2))
    
{
        result[
0= temp;
    }

    
else
    
{
        result[
0= (2*n)/temp;
    }

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

            
else {
                result[count] 
= i;
                count 
++;
            }

        }

    }

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

        cout 
<< "\n\t n=" << result[k] << "\t sum=" << sum << endl;
    }

}


int main()
{
    
int n;
    cout 
<< "input a interge:";
    cin 
>> n;
    compute(n);
    cout 
<< "done";
    
while(1);
    
return 0;
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics