确保变量可被4整除 [英] Ensure a variable is divisible by 4

查看:72
本文介绍了确保变量可被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.

推荐答案



geskerr ... @ hotmail.com schrieb:

geskerr...@hotmail.com schrieb:

我确定这是一个基本的数学问题,但是有更好的方法来确保一个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:


>> x%4
>>x % 4



3

3


ge********@hotmail.com 写道:

我确定这是一个基本的数学问题,但是有更好的方法来确保一个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.



使用模运算符''%''


如果不是x%4:



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




-Larry Bates

Use modulo operator ''%''

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

-Larry Bates


ge********@hotmail.com 写道:

我确定这是一个基本的数学问题,但有没有更好的方法来确保一个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.



如果x%4 == 0:

#x可以整除4

George

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


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

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