1 solutions

  • 0
    @ 2025-7-10 15:45:59

    n&1表示二进制下的最低位 n>>=1,可以遍历二进制下的每一位

    //我是奶龙
    #include <bits/stdc++.h>
    using namespace std;
    int n, ans;
    
    int main() {
    	cin >> n;
    	while (n) {
    		if ((n & 1) == 1) {
    			ans++;
    		}
    		n >>= 1;
    	}
    	cout << ans;
    	return 0;
    }
    
    • 1

    Information

    ID
    301
    Time
    1000ms
    Memory
    256MiB
    Difficulty
    6
    Tags
    # Submissions
    18
    Accepted
    13
    Uploaded By