Skip to content

Instantly share code, notes, and snippets.

@souenzzo
Created June 30, 2016 16:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save souenzzo/9b262602c0b2abfdc84c562b1d1bba92 to your computer and use it in GitHub Desktop.
Save souenzzo/9b262602c0b2abfdc84c562b1d1bba92 to your computer and use it in GitHub Desktop.
Resolução do desafio, sem usar variaveis.
#include <stdio.h>
// Pega o ultimo elemento
int tail(int num, int base) {
return num % base;
}
// Pega o corpo, menos o ultimo elemento
int init(int num, int base) {
return num / base;
}
// Insere no fim
int append(int num, int new, int base) {
return (num * base) + new;
}
int rev(int from, int to, int base) {
if (from == 0)
return to;
else
return rev(init(from, base), append(to, tail(from, base), base), base);
}
int desafio(int i) {
return rev(i, 0, 10);
}
int main (int argc, char **argv) {
if (argc != 2) {
return 1;
}
int n;
sscanf(argv[1], "%d", &n);
printf("%d\n", desafio(n));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment