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;
}