900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > 知乎 ”大家都见过哪些让你虎躯一震的代码?“用户“李晨昊”回答的等价代码

知乎 ”大家都见过哪些让你虎躯一震的代码?“用户“李晨昊”回答的等价代码

时间:2019-09-01 22:42:05

相关推荐

知乎 ”大家都见过哪些让你虎躯一震的代码?“用户“李晨昊”回答的等价代码

下图是这个回答中的代码

原代码:

int merge(int x,int y)

{

return (!x||!y)

?( (x)?(x):(y) )

:( (rand()%(tr[x].siz+tr[y].siz)<tr[x].siz)

?( tr[++tot]=tr[x], x=tot, tr[x].ch[1]=merge(tr[x].ch[1],y), update(x), x)

:( tr[++tot]=tr[y], y=tot, tr[y].ch[0]=merge(x,tr[y].ch[0]), update(y), y)

);

}

等价代码:

int merge(int x,int y)

{

if(x==0||y==0)

{

if(x!=0) return x;

else return y;

}

else

{

if( rand()%(tr[x].siz+tr[y].siz) < tr[x].siz )

{

tr[++tot]=tr[x];

x=tot;

tr[x].ch[1]=merge( tr[x].ch[1] , y );

update(x);

return x;

}

else

{

tr[++tot]=tr[y];

y=tot;

tr[y].ch[0]=merge( x , tr[y].ch[0] );

update(y);

return y;

}

}

}

原代码:

void split(int x,int e)

{

(!x)

?(l=r=0)

:(tr[++tot]=tr[x],x=tot,( (tr[tr[x].ch[0]].siz>=e)

?(split(tr[x].ch[0] , e), tr[x].ch[0]=r, r=x)

:(split(tr[x].ch[1] , e-tr[tr[x].ch[0]].siz-1), tr[x].ch[1]=l , l=x)

),update(x)

);

}

等价代码:

void split(int x,int e)

{

if(x==0)

{

l=r=0;

}

else

{

tr[++tot]=tr[x];

x=tot;

if( tr[tr[x].ch[0]].siz >= e)

{

split( tr[x].ch[0] , e );

tr[x].ch[0]=r;

r=x;

}

else

{

split( tr[x].ch[1] , e-tr[tr[x].ch[0]].siz-1 );

tr[x].ch[1]=l;

l=x;

}

update(x);

}

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。