简体   繁体   English

模数除以10的倍数

[英]Modulus division to get multiples of 10

I'm currently doing a practice where I'm given a non-negative number "num", and return true if num is within 2 of a multiple of 10. 我目前正在做一个练习,给我一个非负数“ num”,如果num在10的倍数的2以内,则返回true。

I have the idea of an if statement which would include the below, but a more working / similar version; 我有一个if语句的想法,其中将包括以下内容,但工作/相似的版本更多;

if (num-2 >= (num % 10 == 0) <= num+2);

Seems like I'm trying to create a variable with all multiples of 10, however this seems a bit overkill. 似乎我正在尝试创建所有10的整数倍的变量,但这似乎有点过分。 I feel it's basic arithmetic with something like num-2 >= x <= num+2, where x is a multiple of 10, but what is the solution? 我觉得这是基本的算术运算,例如num-2> = x <= num + 2,其中x是10的倍数,但是解决方案是什么?

您正在寻找以0、1、2、8或9结尾的数字

if (num % 10 <= 2 || num % 10 >= 8)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM