C ++ Help:数字总和可被n整除的数字 [英] C++ Help: numbers whose sum of the digits is divisible by n

查看:67
本文介绍了C ++ Help:数字总和可被n整除的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

数字的总和可以被n整除的数字。



i真的不明白它应该如何工作,但是每个数字都应该用数字和那么这些数字的总和应该是否可被n整除。 (这只是猜测)



非常感谢任何帮助。

numbers whose sum of the digits is divisible by n.

i dont really understand how it`s supposed to work, but every number should be divided in digits and then the sum of those digits should be or not be divisible by n. (it is only a guess)

Any help is highly appreciated.

推荐答案

分为数字是非常简单:你需要的只是一个循环检查还剩下一个值,如果有,下一个数字就是基数的值模数,下一个数值变成当前值除以相同的基数(显然,这对整数最有效!)

Dividing into digits is pretty simple: all you need is a loop checking that there is still a value left, and if there is, the next digit is just the value modulus the base, and the next value becomes the current value divided by the same base (obviously, this works best with integers!)
digit = value % 10;
value /= 10;



然后你所要做的就是添加数字...



检查可分性更容易:x可以被x除以y模数y为零


Then all you have to do is add the digits...

Checking divisibility is even easier: x is divisible by y of x modulus y is zero


这篇关于C ++ Help:数字总和可被n整除的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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