POJ C + + programming Problem # # 3 of the sum of the low-bit of a computed array

Source: Internet
Author: User

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&sum;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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.