本文介绍了确保变量可被4整除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确信这是一个基本的数学问题,但有没有更好的方法来确保一个int变量可以被4整除,而不是通过执行以下操作来获得;


x = 111

x =(x / 4)* 4


对我来说似乎有点笨拙。

I am sure this is a basic math issue, but is there a better way to
ensure an int variable is divisible by 4 than by doing the following;

x = 111
x = (x /4) * 4

Just seems a bit clunky to me.

推荐答案



休息师:

Division with rest:



3

3




使用模运算符''%''


如果不是x%4:



#如果x是模4可分,则到达这里




-Larry Bates

Use modulo operator ''%''

if not x % 4:
#
# Arrive here if x is modulo 4 divisable
#

-Larry Bates




如果x%4 == 0:

#x可以整除4

George

if x % 4 == 0:
# x is divisible by 4
George


这篇关于确保变量可被4整除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 07:50