在C中,最好的方法是查看一个数字是否可以被另一个整数整除?
在C语言中,哪一种是最好的方法来查看一个数字是否可以被另一个整数整除?我用这个:
Which is the best way, in C, to see if a number is divisible by another? I use this:
if (!(a % x)) {
// this will be executed if a is divisible by x
}
反正还有更快吗?我知道这样做,即130%13将导致每10次做130/13.因此,有10个周期只需要一个周期(我只想知道130是否可被13整除).
Is there anyway which is faster? I know that doing, i.e, 130 % 13 will result into doing 130 / 13 per 10 times. So there are 10 cycles when just one is needed (I just want to know if 130 is divisible by 13).
谢谢!
我知道,做130%13将导致每10次做130/13
I know that doing, i.e, 130 % 13 will result into doing 130 / 13 per 10 times
Balderdash. %
在我使用过的任何处理器上均不执行此操作.它只执行一次130/13
,然后返回其余的值.
Balderdash. %
does no such thing on any processor I've ever used. It does 130/13
once, and returns the remainder.
使用%
.如果您的应用程序运行太慢,请对其进行概要分析并修复所有太慢的问题.
Use %
. If your application runs too slowly, profile it and fix whatever is too slow.