11 Ocak 2013 Cuma

Binary Conversion

Problem Description

Convert two binary numbers into two decimal numbers and compute their sum. Your program has to convert two binary numbers b1, b2 of 8 bits into two decimal numbers d1, d2 respectively. Then compute the result of (d1+d2).

Input File Format

The input consists of N cases. The first line of the input contains only one positive integer N indicating the number of test cases, followed by N following cases. Each case is exactly in one line with two binary numbers b1, b2 (at most 8 digits, and each digit is either 0 or 1) separated by one space. Note that 1 ? N ? 2147483647.

Output Format

For each case, print the result in one line.
Sample Input:
2
01010000 00000001
11110000 10000000
Sample Output:
81
368


  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <math.h>  
  4. void binary(int *result);  
  5. int main(int argc, char *argv[])  
  6. {  
  7.     int i,k,a=0,b=0;  
  8.     char c;  
  9.     scanf("%d%*c",&k);  
  10.     for(i=0;i<k;i++){  
  11.         a=0, b=0;  
  12.         binary(&a);  
  13.         getchar();  
  14.         binary(&b);  
  15.         getchar();  
  16.         printf("%d\n",a+b);  
  17.     }  
  18.     return 0;  
  19. }  
  20. void binary(int *result){  
  21.     char c;  
  22.     int i=0;  
  23.     for(i=0;i<8;i++){  
  24.     scanf("%c",&c);  
  25.     *result+=(int)floor(pow(2,7-i))*(c-'0');  
  26.     }  
  27. }  

Hiç yorum yok:

Yorum Gönder

Binary Conversion

Problem Description Convert two binary numbers into two decimal numbers and compute their sum. Your program has to convert two binary number...