+ 1
Count up in base 3
I am writing a java program, where I have an array, which I need to count up in base 3 sequence. eg. if the size of the array is 4, I would like to change it in following sequence- 0 0 0 0 0 0 0 1 0 0 0 2 0 0 1 0 0 0 1 1 0 0 1 2 0 0 2 0 0 0 2 1 0 0 2 2 0 1 0 0 0 1 0 1 . . 2 2 2 2 How to achieve this?
2 Réponses
0
Did this-
static int[] IncIndexer(int[] indexer) {
for(int i=indexer.length-1;i>=0;i--){
if(indexer[i]==2) { indexer[i]=0; }
else { indexer[i]++; break;}
}
return indexer;
}
+ 1
You can also do the counting with a base 10 number, then translate each number to base 3. One way for changing the base is:
https://code.sololearn.com/caG2AJ8WF88Q/?ref=app