ZOJ.2952 Find All M^N Please【数学】 2015/10/20

Find All M^N Please

Time Limit: 2 Seconds      Memory Limit: 65536 KB

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

感觉题目略坑啊,自己写的怎么都过不了,别人的就A了,也没多大区别啊

//自己的   WA
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>

using namespace std;

int p[50000];

int main(){
    long long i,k=0;
    long long ret = 2147483648;
    for( i = 2 ; i*i <= ret ; ++i ){
        long long ans = i*i;//printf("--------=%d\n",i);
        while( ans <= ret ){
            p[k++] = ans;//printf("=====%d\n",k);
            ans*=i;
        }
    }
    sort(p,p+k);
    printf("%d\n",p[0]);
    for( i = 1 ; i < k ; ++i ){
        if( p[i] != p[i-1] )
            printf("%d\n",p[i]);
    }
    //printf("%d\n",k);
    return 0;
}
//别人的   AC
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<iomanip>
#include<string>
#include<algorithm>
#include<ctime>
#include<stack>
#include<queue>
#include<vector>
#define N 50000
#define MAX 2147483648
using namespace std;
int main()
{

    int i,j,t=(int)sqrt((double)MAX),k=0,ans[N];
    for(i=2;i<=t;i++)
    {
           for(j=2;pow((double)i,(double)j)<MAX;j++)//注意这个条件 
           {
                ans[k++]=pow((double)i,(double)j);                 
           }                   
    }
    sort(ans,ans+k);//排序 
    cout<<ans[0]<<endl;
    for(i=1;i<k;i++)
    {
         if(ans[i]!=ans[i-1])//去重 
            cout<<ans[i]<<endl;  
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值