From 8833b7a5b246cad02e253838c07ba1689834e523 Mon Sep 17 00:00:00 2001 From: Ian C Date: Thu, 9 Dec 2021 13:32:48 +0000 Subject: Initial checkin --- 3.c | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 3.c (limited to '3.c') diff --git a/3.c b/3.c new file mode 100644 index 0000000..39db01d --- /dev/null +++ b/3.c @@ -0,0 +1,75 @@ +#include +#include +#include + +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; +} -- cgit v1.3