华为OD机考题(HJ62 查找输入整数二进制中1的个数)

前言

经过前期的数据结构和算法学习,开始以OD机考题作为练习题,继续加强下熟练程度。

描述

输入一个正整数,计算它在二进制下的1的个数。

注意多组输入输出!!!!!!

数据范围: 1≤𝑛≤231−1 1≤n≤231−1 

输入描述:

输入一个整数

输出描述:

计算整数二进制中1的个数

示例1

输入:

5
输出:

2
说明:

5的二进制表示是101,有2个1

实现原理与步骤

最简单的可以通过Java自带API实现

实现代码(API)

public class CountBits {
    public static void main(String[] args) {
        int number = 29; // Example number
        int count = Integer.bitCount(number);
        System.out.println("Number of 1s in binary representation of " + number + " is: " + count);
    }
}

实现代码(逐位检查)

public class CountBits {
    public static void main(String[] args) {
        int number = 29; // Example number
        int count = countBits(number);
        System.out.println("Number of 1s in binary representation of " + number + " is: " + count);
    }

    public static int countBits(int number) {
        int count = 0;
        while (number != 0) {
            count += number & 1; // Check the least significant bit
            number >>= 1; // Right shift by 1
        }
        return count;
    }
}

实现代码(位清零算法)

public class CountBits {
    public static void main(String[] args) {
        int number = 29; // Example number
        int count = countBits(number);
        System.out.println("Number of 1s in binary representation of " + number + " is: " + count);
    }

    public static int countBits(int number) {
        int count = 0;
        while (number != 0) {
            number &= (number - 1); // Clear the least significant bit set
            count++;
        }
        return count;
    }
}

实现代码(递归算法)

public class CountBits {
    public static void main(String[] args) {
        int number = 29; // Example number
        int count = countBits(number);
        System.out.println("Number of 1s in binary representation of " + number + " is: " + count);
    }

    public static int countBits(int number) {
        if (number == 0) {
            return 0;
        } else {
            return (number & 1) + countBits(number >> 1);
        }
    }
}

相关推荐

  1. 华为OD考题(HJ62 查找输入整数二进制1个数)

    2024-07-19 15:28:02       22 阅读
  2. 华为OD考题HJ1 字符串最后一个单词长度

    2024-07-19 15:28:02       29 阅读
  3. 华为OD考题(HJ108 求最小公倍数)

    2024-07-19 15:28:02       23 阅读
  4. 华为OD考题(HJ90 合法IP)

    2024-07-19 15:28:02       26 阅读
  5. 华为OD考题(HJ6 质数因子)

    2024-07-19 15:28:02       23 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-19 15:28:02       70 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-19 15:28:02       74 阅读
  3. 在Django里面运行非项目文件

    2024-07-19 15:28:02       62 阅读
  4. Python语言-面向对象

    2024-07-19 15:28:02       72 阅读

热门阅读

  1. celery config_from_object的简单使用

    2024-07-19 15:28:02       21 阅读
  2. python程序设定定时任务

    2024-07-19 15:28:02       19 阅读
  3. 【电子数据取证】从SSH开始使用Linux

    2024-07-19 15:28:02       22 阅读
  4. axios源码分析与模拟(上)

    2024-07-19 15:28:02       20 阅读
  5. c语言(7.19)

    2024-07-19 15:28:02       23 阅读
  6. 关于UniApp使用的个人笔记

    2024-07-19 15:28:02       18 阅读
  7. Qt之基础体系

    2024-07-19 15:28:02       16 阅读
  8. Git笔记

    Git笔记

    2024-07-19 15:28:02      20 阅读