码迷,mamicode.com
首页 > 其他好文 > 详细

ZOJ 2952 Find All M^N Please(数学啊 )

时间:2015-04-06 23:23:34      阅读:342      评论:0      收藏:0      [点我收藏+]

标签:zoj   数学   

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1951


Recently, Joey has special interest in the positive numbers that could be represented as M ^ N (M to the power N), where M and N are both positive integers greater than or equal to 2. For example, 4, 8, 9 and 16 are first four such numbers, as 4 = 2 ^ 2, 8 = 2 ^ 3, 9 = 3 ^ 2, 16 = 2 ^ 4. You are planning to give Joey a surprise by giving him all such numbers less than 2 ^ 31 (2147483648). List them in ascending order, one per line.

Sample Output

4
8
9
16
25
27
32
|
| <-- a lot more numbers
|
1024
1089
1156
1225
1296
1331
|
|
|


Author: SHEN, Guanghao
Source: Zhejiang University Local Contest 2008


代码如下:

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cmath>
#define MAX 2147483648
using namespace std;
long long a[400000];
int main()
{
    int i,j,k,x = 0;
    long long s;
    for(i = 2 ; i <= sqrt(MAX) ; i++ )
    {
        for(j = 2; j <= 31 ; j++ )
        {
            s = 1;
            for( k = 1 ; k <= j ; k++)
                s*=i;
            if( s >= MAX )
                break;
            else
                a[x++] = s;
        }
    }
    sort(a,a+x);
    printf("%d\n",a[0]);
    for(i = 1 ; i < x ; i++)
    {
        if(a[i]!=a[i-1])
            printf("%lld\n",a[i]);
    }
    return 0;
}


ZOJ 2952 Find All M^N Please(数学啊 )

标签:zoj   数学   

原文地址:http://blog.csdn.net/u012860063/article/details/44906739

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!