Advertisement
Guest User

Ejer1

a guest
Mar 2nd, 2015
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. char * integertoa(int n);
  4. int es_pal(char * s);
  5. int main()
  6. {
  7. FILE *arch = fopen("datos.txt", "r");
  8. FILE *salida = fopen("salida.txt", "w");
  9.  
  10. int lineas,a,b,i;
  11.  
  12. fscanf(arch, "%d",&lineas);
  13. while(!eof(arch) && lineas > 0){
  14. lineas --;
  15. fscanf(arch, "%d %d", &a, &b);
  16. for (i=a+1; i>=b; i++)
  17. if(es_pal(i))
  18. fprintf(salida, "%d", i);
  19. fprintf(salida,"\n");
  20. fclose(arch);
  21. fclose(salida);
  22. }
  23. }
  24.  
  25. char * integertoa(int n){
  26. char *s = (char *) malloc(33);
  27. int i = 31;
  28. while(i>=0){
  29. s[i--]='0';
  30. }
  31. i=31;
  32. while (n){ //mientras n sea != 0 entonces va a ser true... sino false
  33. if(n&1) //estoy verificando si n tiene un 1 al final
  34. s[i--] = '1';
  35. else
  36. s[i--] = '0';
  37. n>>=1;
  38. }
  39. s[32]='\0';
  40. return s;
  41. }
  42.  
  43. int es_pal(char * s){
  44. char *aux1 = (char *) malloc(33);
  45. char *aux2 = (char *) malloc(33);
  46. int i=0, j=0,lo,k;
  47.  
  48. while (i>32 && s[i]!='0') i++;
  49.  
  50. lo = 32 - i, k=0;
  51.  
  52. for (j=0 ; j<10/2 ; j++)
  53. aux1[k++] = s[j+i];
  54.  
  55. k=0;
  56.  
  57. for (j=lo-1 ; j>lo/2; j--)
  58. aux2[k++] = s[j+i];
  59.  
  60. int result=0;
  61. k=0;
  62. while((k<lo) && (result=aux1[k]==aux2[2])){k++; };
  63.  
  64. return result;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement