Programming Question # #: Calculates the sum of the lower 3 bits of an array
Source: Peking University Online Program evaluation system POJ (Coursera statement: The exercises completed on POJ will not count to the final results of Coursera. )
Total time limit: 1000ms memory limit: 1024kB
Describe
Enter an array of positive integers a[0], a[1], a[2], ..., a[n-1], and calculate the sum of their binary low 3-bit.
#include <iostream> #include <vector> #include <algorithm>using namespace std;//here to complement your code int main ( int argc, char* argv[]) { int V, my_sum=0; Vector<int> Vec; cin>>v; while (v) { vec.push_back (v); cin>>v; } For_each (Vec.begin (), Vec.end (), Cmy_add (My_sum)); cout<<my_sum<<endl; return 0;}
Input
Array A, which indicates the end of the input as 0.
Output
An integer that is the sum of the binary low 3 bits of each element of the input array.
Sample input
1 3 9 7 3 6 20 15 18 17 4 8 18 0
Sample output
41
1#include <iostream>2#include <vector>3#include <algorithm>4 using namespacestd;5 //Add your code here6 classCmy_add {7 Private:8 int∑9 Public:TenCmy_add (int&N): Sum (n) {} One void operator()(intx) { A while(x >=8) { -X-=8; - } theSum + =x; - } - }; - intMainintargcChar*argv[]) { + intV, my_sum=0; -vector<int>Vec; +Cin>>v; A while(v) { at Vec.push_back (v); -Cin>>v; - } - For_each (Vec.begin (), Vec.end (), Cmy_add (My_sum)); -cout<<my_sum<<Endl; - return 0; in}
POJ C + + programming Problem # # 3 of the sum of the low-bit of a computed array