你“在visual studio里头试了很多答案”都是无效的,因为你还不知道二进制补码表示是什么,就不知道题目在考什么. #include <iostream> #include <string> #include <climits> using namespace std; int main() { int i=0; long long n,x; while(cin>>n) { i=0; x=n; if (x == std::numeric_limits<long long="">::min()) { cout << "1\n"; continue; } if (x < 0) { x += (1ull<<63); ++i; } while(x) { if(x&1) i++; x>>=1; } cout<<</long></climits></string></iostream>