关键在于 n%k n=n/k #include<vector> #include<cmath> #include<iostream> #include<cstdio> using namespace std; int main() { int n; vector<int> vec; while (cin >> n) { vec.clear(); //vec初始化 while (n != 0) { vec.push_back(n % 2); n = n / 2; } for (int i = vec.size() - 1; i >= 0; i--) { cout << vec[i]; } cout << endl; } }