首页 > 解决方案 > C#模数方程返回错误值

问题描述

var newPosition = (position - key) % alphabet.Length;

当 position 是 66,key 是 7964 并且 alphabet.length = 91,newPosition 是-72,即使它应该是 19。为什么?

标签: c#modulus

解决方案


((position - key) % alphabet.Length + alphabet.Length) % alphabet.Length

做到了


推荐阅读