Java Reference
In-Depth Information
< Day Day Up >
Puzzle 64: The Mod Squad
This program generates a histogram of the numbers mod 3. What does it print?
public class Mod {
public static void main(String[] args) {
final int MODULUS = 3;
int[] histogram = new int[MODULUS];
// Iterate over all ints (Idiom from Puzzle 26)
int i = Integer.MIN_VALUE;
do {
histogram[Math.abs(i) % MODULUS]++;
} while (i++ != Integer.MAX_VALUE);
for (int j = 0; j < MODULUS; j++)
System.out.println(histogram[j] + " ");
}
}
 
 
Search WWH ::




Custom Search