Created
March 25, 2014 06:45
-
-
Save leino/9756342 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int powers_recursive(int b, int l){ | |
if(l>0){ | |
for(int i=0; i<b-1; ++i){ | |
int p = powers_recursive(b, l-1); | |
printf("%d", p); | |
} | |
powers_recursive(b,l-1); | |
return l; | |
}else{ | |
return 0; | |
} | |
} | |
void powers_loop_goto(int b, int l){ | |
int reps[l]; | |
int p = l-1; | |
reps[p] = b; | |
while(p < l){ | |
while(reps[p] > 0){ | |
--reps[p--]; | |
if( p == -1 ) goto inc; | |
reps[p] = b; | |
} | |
while(reps[p] == 0 && p < l) | |
inc: ++p; | |
printf("%d", p); | |
} | |
} | |
void powers_loop(int b, int l){ | |
int reps[l+1]; | |
int p = l; | |
reps[p] = b; | |
while(p <= l){ | |
while(reps[p] > 0){ | |
--reps[p--]; | |
reps[p] = (p == 0) ? 0 : b; | |
} | |
while(reps[p] == 0 && p <= l) | |
++p; | |
printf("%d", p-1); | |
} | |
} | |
void powers_loop_break(int b, int l){ | |
int reps[l]; | |
int p = l-1; | |
reps[p] = b; | |
while(p < l){ | |
while(reps[p] > 0){ | |
--reps[p]; | |
if(p == 0) break; | |
--p; | |
reps[p] = b; | |
} | |
while(reps[p] == 0 && p < l) | |
++p; | |
printf("%d", p); | |
} | |
} | |
void powers_loop_slick(int b, int l){ | |
int reps[l]; | |
int p = l-1; | |
reps[p] = b; | |
while(p < l){ | |
while(reps[p]-- > 0 && p > 0) | |
reps[--p] = b; | |
while(reps[p] == 0 && p < l) | |
++p; | |
printf("%d", p); | |
} | |
} | |
int main(){ | |
int b = 6; int l = 3; | |
// recursive | |
powers_recursive(b,l); printf("%d", l); | |
printf("\n"); | |
// loop + goto | |
powers_loop_goto(b,l); | |
printf("\n"); | |
// loop + break | |
powers_loop_break(b,l); | |
printf("\n"); | |
powers_loop_slick(b,l); | |
printf("\n"); | |
// loop | |
powers_loop(b,l); | |
printf("\n"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment