[Title] Netease programming to Dr. Niu Xiao Yi out a problem

Title:
Xiao Yi has a length of N is a positive integer number sequence A = {A [1], A [2], A [3] ..., A [N]}.
Dr. cattle for small prone a problem:
number of columns A rearranged so that the number of column A satisfy all of A [i] * A [i + 1] (1 ≤ i ≤ N - 1) is a multiple of four.
Xiao Yi now need to determine whether a number of columns can be rearranged to meet the requirements after Dr. cattle.


Ideas:
SUM 0 is the initial
statistics for each number:

  1. If divisible by 4, the sum + = 2;
  2. If not divisible by 4, but divisible by 2, is referred to as a sum + = 1;
  3. If not divisible by 2, is referred to as a sum + = 0;

If the final sum is greater than equal to the array size, compared with Yes, otherwise No. Do not ask me why, I was blind in misty.


#include <iostream>
#include <vector>
using namespace std;

bool f(vector<int> &vec){
    int sumof2 = 0;
    for (int i = 0; i < vec.size(); ++i){
        int max = 2;
        while (max && vec[i]%2 == 0){
            sumof2 += 1;
            --max;
            vec[i] /= 2;
        }
    }
    
    return sumof2 >= vec.size();
}

int main(){
    int total;
    cin >> total;
    for (int i = 0; i < total; ++i){
        int n;
        vector<int> vec;
        cin >> n;
        for (int j = 0; j < n; ++j){
            int t;
            cin >> t;
            vec.push_back(t);
        }
        
        if (f(vec)){
            cout << "Yes"<<endl;
        }else{
            cout << "No"<<endl;
        }
        
    }
    
    return 0;
}
Published 92 original articles · won praise 2 · Views 3403

Guess you like

Origin blog.csdn.net/zxc120389574/article/details/105318257