这题 看上去 很无聊 但还是几乎花了一部 小电影 的时间=-=
题意 没什么好讲的.... 就是找 平方数
一开始 用了2个数字 分别标记与存储数据 但发现 inf = 2147483648 数据太大了
如果直接存储 平方数 也是很麻烦的 开始就把时间花这了 。。。
然后用map --- 不得不说 红黑树 这厉害 log(n)的时间 不是假的
我还是喜欢自己手写 pow 函数 而不是调用 cmath中的pow
这边 好像 其实 mp.find(X) == mp.end() 与 mp.count(x) 其实 在 时间运行上 是差不多的
好了 直接上code
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <map> 3 using namespace std; 4 5 #define LL long long 6 const long long inf = 2147483648; 7 map<LL,LL>mp; 8 9 LL pow( LL x , LL n ) 10 { 11 LL y = x; 12 while( --n ) 13 { 14 x*=y; 15 } 16 return x; 17 } 18 19 void init() 20 { 21 LL temp; 22 for( LL i = 2 ; i*i<=inf ; i++ ) 23 { 24 for( LL j = 2 ; j<=30 ; j++ ) 25 { 26 temp = pow( i , j ); 27 if( temp>=inf ) 28 { 29 break; 30 } 31 else 32 { 33 if( mp.find(temp) == mp.end() ) 34 mp[temp] = 1; 35 } 36 } 37 } 38 } 39 40 void print() 41 { 42 map<LL,LL>::iterator it; 43 for( it = mp.begin() ; it!=mp.end() ; it++ ) 44 { 45 printf( "%I64d ",it->first ); 46 } 47 } 48 49 int main() 50 { 51 init(); 52 print(); 53 getchar(); 54 return 0; 55 }
美国往事 似乎 蛮好看 =-= 看过的 说下..