Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- int
- fib_get(int n)
- {
- static int fib[21] = { 0, 1 }; // fib(21) = 10946, fib(20) = 6765, max string length * bytesize = 8000
- static int fi = 2;
- if(n == 0)
- return 0;
- while(!fib[n])
- fib[fi++] = fib[fi - 2] + fib[fi - 3];
- return fib[n];
- }
- char *
- ch_to_bin(char *buf, char ch)
- {
- for(int i = 7; i >= 0; i--, buf++)
- *buf = ch & 1 << i ? '1' : '0';
- return buf;
- }
- void
- build(char *buf, char *src)
- {
- while(*src)
- buf = ch_to_bin(buf, *(src++));
- }
- int
- is_fib(size_t n)
- {
- for(int i = 0; i <= 21; i++)
- if(n == fib_get(i))
- return 1;
- return 0;
- }
- void
- parse(char *src)
- {
- int total = 0;
- for(int i = strlen(src) - 1; i >= 0; i--)
- if(is_fib(i))
- total += src[i] - '0';
- printf("%u\n", total);
- }
- int
- main()
- {
- char text[502],
- binary[8001] = ""; // max string length * bytesize = 8000
- fgets(text, 501, stdin);
- build(binary, text);
- parse(binary);
- return 0;
- }
- /*
- FIB sequence = [0 1 2 3 5 8 ...]
- "testcase" = 74 65 73 74 63 61 73 65
- 74 0111 0100
- 65 0110 0101
- 73 0111 0011
- 74 0111 0100
- 63 0110 0011
- 61 0110 0001
- 73 0111 0011
- 65 0110 0101
- 0111 0100
- 0110 0101
- 0111 0011
- 0111 0100
- 0110 0011
- 0110 0001
- 0111 0011
- 0110 0101
- 7654 3210
- 01 [1] 1 [0100]
- 01 [1] 0 [0101]
- 01 [1] 1 [0011]
- 01 [1] 1 [0100]
- 01 [1] 0 [0011]
- 01 [1] 0 [0001]
- 01 [1] 1 [0011]
- 01 [1] 0 [0101]
- 76 5 4 3210
- ^ ^^^^ < add all these together
- expect: 7
- got: 6 + 3 + 3 + 4 + 0 + 8 = 24 ... huh?
- [0111 0100] 0 < 4
- [0110 0101] 1 < 4
- [0111 0011] 2 < 5
- [0111 0100] 3 < 4
- 0110 0011 4
- [0110 0001] 5 < 3
- 0111 0011 6
- 0110 0101 7
- also more than 7. Not sure what to do. The description is too vague. Lemme try both.
- 0111 0100 0 < 2 (2)
- 0110 0101 1 < 3 (5)
- 0111 0011 2 < 3 (8) !
- 0111 0100 3 < 4
- 0110 0011 4
- 0110 0001 5 < 3
- 0111 0011 6
- 0110 0101 7
- 7654 3210
- ^ ^^^^
- Someone in the comments said something about a stream of bytes, so what if I...
- 0111 0100 0110 0101 0111 0011 0111 0100 0110 0011 0110 0001 0111 0011 0110 0101
- ^ ^ ^ ^ ^ ^ ^^^^
- 1 + 0 + 0 + 1 + 0 + 1 + 1 + 1 + 1 + 1 + 0 = 7
- A-ha. Now to implement...
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement