
🎈🎈 作者 whispar
🎈🎈专栏 : 小题练手

活动地址:CSDN21天学习挑战赛
给定一个整数n,返回该整数二进制形式1的个数。
方法一:将三十二位二进制数字分别与1的二进制形式相与,由 0 & 1 = 0 1 & 1 = 1
思路:将该数字的每一位与32位的1相与,使用移位运算每次移动一位二进制数字
如:
n = 0011 0010 & 0000 0001 count = 0 n>>1
n = 0001 1001 & 0000 0001 count = 1 n>>1
以此类推
- public static void main2(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int n = scanner.nextInt();
- int count = 0;
- for (int i = 0; i < 32; i++) {
- if (((n >> i) & 1) != 0) {
- count++;
- }
- }
- System.out.println(count);
- }
缺陷:每个数都要按位与完32位,存在重复比较的情况,增加一个判断条件即可
- /**
- * 移动的过程中判断 n 是否为 0
- * 无循环判断32次,所以使用无符号右移
- * @param args
- */
- public static void main5(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int n = scanner.nextInt();
- int count = 0;
- while(n!=0){
- if((n&1) != 0){
- count++;
- }
- n = n >>> 1;
- }
- System.out.println(count);
- }
- }
此种情况下,避免了移动一定位数后n的左侧全为0的情况
方法二:
思路:相邻的两个数按位与运算,每相与一次即减少一个一,得到的即为大数中1的个数
10= 0000 1010 & 9 = 0000 1001 --> 0000 1000 count =1
0000 1000 & 0000 0111 = 0000 0000 count = 2
- /**
- * n与n-1相与,每相与一次即少一个一,次数即n中1的个数
- */
- public static void main4(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int n = scanner.nextInt();
- int count = 0;
- while (n != 0) {
- n = n&(n-1);
- count++;
- }
- System.out.println(count);
- }
编写代码模拟三次密码输入的场景。 最多能输入三次密码,密码正确,提示“登录成功”,密码错误, 可以重新输 入,最多输入三次。三次均错,则提示退出程序
- public static void main(String[] args) {
- Scanner s =new Scanner(System.in);
- int count = 3;
- while(count != 0){
- System.out.println("请输入你的密码,还有"+count+"次机会");
- String pass = s.nextLine();
- if(pass.equals("123")) {
- System.out.println("登录成功");
- break;
- }else{
- System.out.println("登录失败");
- count--;
- }
- }
- }
获取一个数二进制序列中所有的偶数位和奇数位, 分别输出二进制序列
思路:提取所有的奇数位和偶数位,分别与1相与,结果为1则1,为0则0
- /**
- * 获取一个数二进制序列中所有的偶数位和奇数位, 分别输出二进制序列
- */
- public static void main(String[] args) {
- Scanner s = new Scanner(System.in);
- int n =s .nextInt();
- for (int i = 31; i >= 1 ; i-=2) {
- System.out.print(((n>>i)&1)+" ");
- }
- System.out.println();
- for (int i = 30; i >= 0 ; i-=2) {
- System.out.print(((n>>i)&1)+" ");
- }
- }

调整数组顺序使得奇数位于偶数之前。调整之后,不关心大小顺序。如数组:[1,2,3,4,5,6],调整后可能是:[1, 5, 3, 4, 2, 6]
思路:保持 i < j 判断奇偶,交换位置,使奇数位于偶数之前
- public static void func2(int[] array){
- int i= 0;
- int j =array.length-1;
- while(i
- while(i
2 != 0){ - i++;
- }
- while(i
2 == 0){ - j--;
- }
- int tmp = array[i];
- array[i] = array[j];
- array[j] =tmp;
- }
- }
题目五
给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标。
思路:暴力解法,使用双层循环解题
- class Solution {
- public int[] twoSum(int[] nums, int target) {
- int[] nums2 =new int[2];
- for (int i = 0; i < nums.length; i++) {
- for (int j = i+1; j < nums.length && j!=i ; j++) {
- if (nums[i] + nums[j] == target) {
- nums2[0]=i;
- nums2[1]=j;
- }
- }
- }
- return nums2;
- }
- }
💖如果文章对你有帮助,请多多点赞、收藏、评论、关注支持!!💖
