您的位置:首页 > 其它

TOJ---1502---map真强大

2014-06-22 17:30 190 查看
这题 看上去 很无聊 但还是几乎花了一部 小电影 的时间=-=

      touch me

题意 没什么好讲的.... 就是找 平方数

一开始 用了2个数字 分别标记与存储数据 但发现 inf = 2147483648 数据太大了

如果直接存储 平方数 也是很麻烦的 开始就把时间花这了 。。。

然后用map --- 不得不说 红黑树 这厉害 log(n)的时间 不是假的

我还是喜欢自己手写 pow 函数 而不是调用 cmath中的pow

这边 好像 其实 mp.find(X) == mp.end() 与 mp.count(x) 其实 在 时间运行上 是差不多的

好了 直接上code

#include <iostream>
#include <map>
using namespace std;

#define LL long long
const long long inf = 2147483648;
map<LL,LL>mp;

LL pow( LL x , LL n )
{
LL y = x;
while( --n )
{
x*=y;
}
return x;
}

void init()
{
LL temp;
for( LL i = 2 ; i*i<=inf ; i++ )
{
for( LL j = 2 ; j<=30 ; j++ )
{
temp = pow( i , j );
if(  temp>=inf )
{
break;
}
else
{
if( mp.find(temp) == mp.end() )
mp[temp] = 1;
}
}
}
}

void print()
{
map<LL,LL>::iterator it;
for( it = mp.begin() ; it!=mp.end() ; it++ )
{
printf( "%I64d\n",it->first );
}
}

int main()
{
init();
print();
getchar();
return 0;
}


View Code
美国往事 似乎 蛮好看 =-= 看过的 说下..
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: