Advertisement
Guest User

Untitled

a guest
Jun 30th, 2016
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. // base ^ power
  4. int fpow(int base, int power) {
  5. return (power == 1) ? 1 : base * fpow(base, power - 1);
  6. }
  7.  
  8. // Ultimo digito do numero
  9. int last(int num, int base) {
  10. return num % base;
  11. }
  12.  
  13. // Todos, menos ultimo digito do numero
  14. int init(int num, int base) {
  15. return num / base;
  16. }
  17.  
  18. // Numero de digitos do numero
  19. int len(int num, int base) {
  20. return (num == 0) ? 0 : 1 + len(init(num, base), base);
  21. }
  22.  
  23. // Primeiro digito do numero
  24. int head(int num, int base) {
  25. return num / fpow(base, len(num, base));
  26. }
  27.  
  28. // Todos, menos primeiro digito do numero
  29. int tail(int num, int base) {
  30. return num % fpow(base, len(num, base));
  31. }
  32.  
  33. // Inverte a ordem dos digitos do numero
  34. int rev(int num, int base) {
  35. return (num == 0) ? 0 : head(num, base) + (base * rev(tail(num, base), base));
  36. }
  37.  
  38. // Formata a função de acordo com o desafio (int -> int)
  39. int desafio(int i) {
  40. return rev(i, 10);
  41. }
  42.  
  43. int main (int argc, char **argv) {
  44. if (argc != 2) {
  45. return 1;
  46. }
  47. int n;
  48. sscanf(argv[1], "%d", &n);
  49. printf("%d\n", desafio(n));
  50. return 0;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement