代码之家  ›  专栏  ›  技术社区  ›  AFS

求逆环索引得到最大索引作为C上第一个索引的第一个

  •  0
  • AFS  · 技术社区  · 7 年前

    [0][1][2][3] // the previous index of 0 should be 3
    
    2 回复  |  直到 7 年前
        1
  •  1
  •   trincot    7 年前

    在语言中 C 余数运算符可按如下方式使用,以获得真(正)模结果:

    (index+n-1)%n
    

    N 是数组的长度。

        2
  •  0
  •   Oleg    7 年前

    prev(index) = (index-1) % len(array) 鉴于此 % 返回模数而不是余数(取决于语言 mod 消极的 整数)

    int m(int a,int b){
    return (a % b + b) % b;
    }
    

    prev(n) = m(n-1,len(array))