题解 | #消减整数#
消减整数
https://ac.nowcoder.com/acm/problem/219038
思路
贪心
过程
代码
#include <iostream>
using namespace std;
int t, h;
int func()
{
h --;
int ans = 1, a = 1;
while(h)
{
if(h % (2 * a) == 0)
{
h -= 2 * a;
a *= 2;
}
else h -= a;
ans ++;
}
return ans;
}
int main()
{
cin >> t;
while(t --)
{
cin >> h;
cout << func() << endl;
}
return 0;
}