diff options
Diffstat (limited to '3.c')
-rw-r--r-- | 3.c | 75 |
1 files changed, 75 insertions, 0 deletions
@@ -0,0 +1,75 @@ +#include <stdlib.h> +#include <stdio.h> +#include <string.h> + +static int FromBin(const char *p) +{ + int b = strlen(p) - 1; + int v = 0; + int f; + + for(f = 0; p[f]; f++, b--) + { + if (p[f] == '1') + { + v |= 1 << b; + } + } + + return v; +} + +int main(void) +{ + char buff[80]; + int bit[12] = {0}; + int count = 0; + char str[13] = {0}; + int f; + int gamma, epsilon; + + while(fgets(buff, sizeof buff, stdin)) + { + for(f = 0; f < 12; f++) + { + if (buff[f] == '1') + { + bit[f]++; + } + } + + count++; + } + + for(f = 0; f < 12; f++) + { + if (bit[f] > count / 2) + { + str[f] = '1'; + } + else + { + str[f] = '0'; + } + } + + gamma = FromBin(str); + + for(f = 0; f < 12; f++) + { + if (str[f] == '0') + { + str[f] = '1'; + } + else + { + str[f] = '0'; + } + } + + epsilon = FromBin(str); + + printf("%d\n", gamma * epsilon); + + return 0; +} |