fork download
  1. #include <iostream> // This library is the basic in/out library
  2. //#include <fstream> //This library will allow me to use files
  3. #include <string> //This will allow me to use strings
  4. #include <sstream>
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10. //ifstream infile; //I am renaming the ifstream command to infile since it is easier to remember and us
  11. //ofstream outfile; //I also renamed the ofstream to outfile
  12.  
  13. //infile.open("binary.txt"); //This is opening the binary.txt file which has to be located on the master directory
  14. istream &infile = cin;
  15. int numLength; //This will determine how many digits the binary number will have
  16.  
  17. string line;
  18. while (getline(infile, line))
  19. {
  20. istringstream iss(line);
  21. iss >> numLength;
  22.  
  23. int digit, binary = 0;
  24.  
  25. for (int i = 0; i < numLength; i++)
  26. {
  27. iss >> digit;
  28. if (digit == 1)
  29. binary |= (1 << (numLength - i - 1));
  30. }
  31.  
  32. cout << binary << endl;
  33. }
  34.  
  35. return 0;
  36. }
Success #stdin #stdout 0s 4464KB
stdin
5  1 0 1 1 0
4  1 1 1 0 
7  1 1 1 0 1 0 1
10 1 0 1 1 1 0 0 0 1 0
stdout
22
14
117
738