fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. void reverse(char*s,int n) {
  5. printf("reverse=%s %d\n", s,n);
  6. char t;
  7. while(n>0) {
  8. t=*s;
  9. *s=*(s+n-1);
  10. *(s+n-1)=t;
  11. n-=2;
  12. }
  13. }
  14.  
  15. void f(char*s) {
  16. int len=strlen(s);
  17. char*w=malloc(len*sizeof(char)+2);
  18. sprintf(w,"0%s", s);
  19. char*p=w+len;
  20. int n=0;
  21. while(*p=='0')
  22. --p;
  23. while(*p=='1')
  24. --p,++n;
  25. reverse(p,2);
  26. p+=2;
  27. reverse(p,len-(p-w)+1);
  28. if (*w=='0')
  29. ++w;
  30. printf("input = %s\noutput = %s\n\n", s,w);
  31. }
  32.  
  33. int main()
  34. {
  35. f("111");
  36. f("1110");
  37. f("101100");
  38. f("100011");
  39. f("1000000000000000000000000000000000010100");
  40. f("1000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000001");
  41. return 0;
  42. }
  43.  
Success #stdin #stdout 0s 2380KB
stdin
Standard input is empty
stdout
reverse=0111 2
reverse=11 2
input  = 111
output = 1011

reverse=01110 2
reverse=110 3
input  = 1110
output = 10011

reverse=01100 2
reverse=100 3
input  = 101100
output = 110001

reverse=011 2
reverse=1 1
input  = 100011
output = 100101

reverse=0100 2
reverse=00 2
input  = 1000000000000000000000000000000000010100
output = 1000000000000000000000000000000000011000

reverse=01 2
reverse= 0
input  = 1000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000001
output = 1000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000010