java - Mapping a range of positive integers to range of inidices -
basically, need kind of mapping function should map integer (0 - n) index (0 - m). n = 10, m = 3, function should map: 1 -> 0, 2 -> 1, 3 -> 2, 4 -> 3, 5 -> 0, 6 -> 1, 7 -> 2, 8 -> 3, 9 -> 0 , 10 -> 1.
my brain pretty dead ended bull*&^% mapping :)
public int getindexfornumber(int number, int maxindex) {     int max = maxindex;     while (maxindex > 0) {         if (number % maxindex-- == 0) {             return maxindex;         }     }     return max; } can please direct me?
why don't return remainder?
public int getindexfornumber(int number, int maxindex) {   return (number - 1) % maxindex;  } if negative numbers allowed
public int getindexfornumber(int number, int maxindex) {   int x = (number - 1) % maxindex;     if (x < 0)     x += maxindex;    retrun x;  } 
Comments
Post a Comment