C++ program to find A & B & C | CodeChum Solution | C++ Programming
Problem:
I am the creator of the Truth Machine! It’s a machine that accepts 3 numbers, with each number having an accompanying bit position (since the numbers are going to be in binary). After successfully getting 1 bit from each number, the machine would then process the numbers to give us either a 1 or 0, depending on how truthful the bits processed are. I feel like no one in the world could possibly make something like it again!
Care to prove me wrong?
Input Format:
A single line containing six values: x1, x2, y1, y2, z1, and z2.
x1 represents A.
x2 represents bit position of A (starting 0 from right to left).
y1 represents B.
y2 represents bit position of B (starting 0 from right to left).
z1 represents C.
z2 represents bit position of C (starting 0 from right to left).
Output Format:
A single line containing either a 1 or 0.
Input Sample:
3 1 5 1 3 1
Output Sample:
0
Code:
// A & B & C by CodeChum
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int size(char a[], int n){
int len = 0;
for (int i = 0; i < n; i++){
if (a[i] != '0' && a[i] != '1'){
break;
}
len++;
}
return len;
}
int main(){
int x1, x2, y1, y2, z1, z2;
cin >> x1 >> x2 >> y1 >> y2 >> z1 >> z2;
char a[15], b[15], c[15];
itoa(x1, a, 2);
itoa(y1, b, 2);
itoa(z1, c, 2);
char first = a[size(a, 15)-x2-1];
char second = b[size(b, 15)-y2-1];
char third = c[size(c, 15)-z2-1];
x1 = first; y1 = second; z1 = third;
int result = (x1 & y1 & z1);
cout << (char)result << endl;
return 0;
}
Comments