Created
September 17, 2014 13:52
-
-
Save gyk/8a1f302994379ae21472 to your computer and use it in GitHub Desktop.
Oneliner that counts forward and backward
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> | |
#include <stdlib.h> | |
/******** | |
func(1, 1); -> 1 | |
func(1, 3); -> 1 2 3 2 1 | |
func(1, 6); -> 1 2 3 4 5 6 5 4 3 2 1 | |
`func` should be implemented without any loop, if-statement or | |
conditional operation. | |
********/ | |
int func(int i, int n) | |
{ | |
return printf("%d %.0s", (n - abs(i - n)), i + 1 == n * 2 || func(i + 1, n)); | |
} | |
int main(int argc, char const *argv[]) | |
{ | |
func(1, 1); puts(""); | |
func(1, 3); puts(""); | |
func(1, 6); puts(""); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment