fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. int main(int argc, char **argv) {
  6. static char line[80];
  7.  
  8. while (fgets(line, 80, stdin)) {
  9. char *p = strtok(line, " ");
  10.  
  11. if (p) {
  12. if (strcmp(p, "F") == 0) {
  13. unsigned long n = 0;
  14. p = strtok(NULL, " \r\n");
  15. p += strlen(p);
  16.  
  17. unsigned long a = 1, b = 1, c;
  18. while (*--p) {
  19. if (*p == '1') n += b;
  20.  
  21. // a,b = a+b,a
  22. c = a+b;
  23. b = a;
  24. a = c;
  25. }
  26.  
  27. printf("%lu\n", n);
  28.  
  29. } else {
  30. unsigned long n = strtoul(strtok(NULL, " \r\n"), NULL, atoi(p));
  31.  
  32. unsigned long a = 1, b = 1, c;
  33. size_t i = 0;
  34.  
  35. while (a <= n) {
  36. c = a+b;
  37. b = a;
  38. a = c;
  39. i++;
  40. }
  41.  
  42. char buf[i+1];
  43. i = 0;
  44.  
  45. while (b > 0) {
  46. if (b <= n) {
  47. buf[i++] = '1';
  48. n -= b;
  49. } else {
  50. buf[i++] = '0';
  51. }
  52.  
  53. c = a-b;
  54. a = b;
  55. b = c;
  56. }
  57.  
  58. buf[i] = '\0';
  59.  
  60. p = buf;
  61. while (*p) {
  62. if (!strncmp(p, "100", 3)) {
  63. strncpy(p, "011", 3);
  64. }
  65. p++;
  66. }
  67.  
  68. if (i >= 4) {
  69. p = buf+i-4;
  70. if (!strncmp(p, "1010", 4)) {
  71. strncpy(p, "0111", 4);
  72. }
  73. }
  74.  
  75. p = buf+i;
  76. while (p > buf) {
  77. p--;
  78. if (!strncmp(p, "100", 3)) {
  79. strncpy(p, "011", 3);
  80. }
  81. }
  82.  
  83. printf("%s\n", buf + strspn(buf, "0"));
  84. }
  85. }
  86. }
  87.  
  88. return 0;
  89. }
Success #stdin #stdout 0s 2172KB
stdin
10 8
10 16
10 32
10 9024720
stdout
10110
110110
1111110
111111011111110101101011101101110