牛客编程巅峰赛S1第6场 - 黄金&钻石&王者 A.牛牛爱奇数 (模拟)

  • 题意:有一组数,每次将所有相等的偶数/2,求最少操作多少次使得所有数变成奇数.

  • 题解:用桶标记,将所有不同的偶数取出来,然后写个while模拟统计一下次数就行.

  • 代码:

    class Solution {
    public:
        /**
         * 返回一个数,代表让这些数都变成奇数的最少的操作次数
         * @param n int整型 代表一共有多少数
         * @param a int整型vector 代表n个数字的值
         * @return int整型
         */ 
        map<int,int> mp;
        int solve(int n, vector<int>& a) {
            // write code here
            int cnt=0;
            for(int i=0;i<n;++i){
                while(a[i]%2==0 && !mp[a[i]]){
                    mp[a[i]]++;
                    cnt++;
                    a[i]/=2;
                }
            }
            return cnt;
        }
    };
    

猜你喜欢

转载自www.cnblogs.com/lr599909928/p/13380044.html