【牛客】[编程题]查找输入整数二进制中1的个数 C++
发布日期:2021-05-10 06:32:28 浏览次数:12 分类:精选文章

本文共 1700 字,大约阅读时间需要 5 分钟。

1.������������

������������������������������������������������������������������1���������������������������������������������������������������������������������������������������������������

2.������������

���������������������������������������������������������������������������������������������������������������������������������������������1���������������������������������������������������������������������������������������������������������������������������������������������������������������

1.������������

������������������n���

������������������������1������������

2.������������

   #include 
using namespace std; int main() { int n = 0; while (cin >> n) { int count = 0; while (n) { if (n & 1 == 1) count++; n = n >> 1; } cout << count << endl; } return 0; }

������������

  • ���������������������while (cin >> n)���������������������n���������cin >> n���������������������������������������������������������
  • ���������������������int count = 0; ������������������������1������������
  • ������������������while (n) ������������������������n������0���
    • if (n & 1) ������n���������������������������1������������������������������������
    • n = n >> 1 ���n���������������������������������������������
  • ���������������cout << count << endl; ���������������������������������������1������������
  • ���������������������������������������return 0; ������������������������
  • ���������������������������������������������������������������������������������������������������������������������������������������������������������

    上一篇:【牛客】[编程题] iNOC产品部--完全数计算C++
    下一篇:【牛客】[编程题] 幸运的袋子C++

    发表评论

    最新留言

    关注你微信了!
    [***.104.42.241]2025年04月21日 16时46分34秒