2

Here's the problem - I think it's a very common one with a well documented answer, but I'm not sure what the name of it is to find the answer:

I have a number n(260) between 0 and x(359).

I want to add a value of 100 to n but instead of the result being 360 I want it to cycle back round to 0

It works the other way as well e.g. n minus 270 should equal 349

Any ideas if the algorithm has a name?

4

3 回答 3

1

模运算是解决这个问题的正确方法......

于 2013-10-11T22:19:27.923 回答
1

这是一个模组功能。如果你取 260 mod 360 之类的东西,答案将是 260。如果你取 360 mod 360,你得到 0。它相当于欧几里得除法的余数。

于 2013-10-11T22:28:46.997 回答
0

我想,您是在询问 modul_to 操作或除法提醒。

n_new = (n_old + 100) % (x + 1)
n_new = (260 + 100) % 360

UPD:值(n_old,n_new)需要定义为“unsigned int”,以便始终获得积极的提醒。

于 2013-10-11T22:15:06.543 回答