在C中,最好的方法是查看一个数字是否可以被另一个整数整除? [英] Which is the best way, in C, to see if a number is divisible by another?

查看:91
本文介绍了在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.

这篇关于在C中,最好的方法是查看一个数字是否可以被另一个整数整除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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