〜简单数字问题 [英] ~Simple number problem

查看:50
本文介绍了〜简单数字问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果int介于0到29之间,则返回0.如果30-59,则返回30.

可以使用单行数学表达式,没有IF语句或位来执行此操作

换班? TIA


-

William Stacey [MVP]

if int is between 0-29 return 0. If 30-59, return 30.
Can you do this with a one line math expression and no IF statements or bit
shifting? TIA

--
William Stacey [MVP]

推荐答案

int i =(value / 30)* 30;

int i = (value / 30) * 30;


Math.Floor(30 / x)* 30;


哪个比CPU好得多?


y =(x> 30?30:0);


是否存在这些限制的原因(如果 - 等等......)或这只是

智力运动?


William Stacey [ MVP] QUOT; <无线************ @ gmail.com>在消息中写道

news:uS ************** @ tk2msftngp13.phx.gbl ...
Math.Floor(30 / x)*30;

Which is much more expensive CPU-wise than

y = (x > 30 ? 30 : 0);

Is there a reason for these constraints (no if-then etc...) or is this just
intellectial exercise?

"William Stacey [MVP]" <wi************@gmail.com> wrote in message
news:uS**************@tk2msftngp13.phx.gbl...
如果int介于0之间-29返回0.如果30-59,则返回30.
你可以用一行数学表达式,没有IF语句或
位移动吗? TIA

-
William Stacey [MVP]
if int is between 0-29 return 0. If 30-59, return 30.
Can you do this with a one line math expression and no IF statements or
bit
shifting? TIA

--
William Stacey [MVP]



> Math.Floor(30 / x)* 30;
> Math.Floor(30 / x)*30;
这比(/ x> 30?30:0)更加昂贵的CPU( 30?30:0);
Which is much more expensive CPU-wise than
y = (x > 30 ? 30 : 0);




实际上我应该关闭这个...编译器团队可能在库中有一个

超快速的Floor()实现(即,强制转换) to int)和

我只是在这里陈述未经证实的想法......



Actually I should shut up about this... The compiler team might have a
super-fast implementation of Floor() in the library (ie, cast to int) and
I''m just stating unsubstantiated ideas here...


这篇关于〜简单数字问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆