#include <iostream> using namespace std; int main() { int n, m; while (cin >> n >> m) { if (!n && !m) break; int sum = 0; for (int i = 0; i < m; ++i) { int k = n - i; while (!(k & 1)) { k >>= 1; sum++; } } cout << sum << endl; } }
简洁的位运算
#include <iostream> using namespace std; int main() { int n, m; while (cin >> n >> m) { if (!n && !m) break; int sum = 0; for (int i = 0; i < m; ++i) { int k = n - i; while (!(k & 1)) { k >>= 1; sum++; } } cout << sum << endl; } }
简洁的位运算