• 【小题练手】---Java基础


    ced485cbb11e458d81a746890b32cf3f.gif

       🎈🎈 作者 whispar

    🎈🎈专栏 : 小题练手

    7abc9c8906564477a6679bb15d368e37.gif

    活动地址:CSDN21天学习挑战赛

     题目一:

    CD145 整数的二进制数表达中有多少个1

    给定一个整数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

    以此类推

    1. public static void main2(String[] args) {
    2. Scanner scanner = new Scanner(System.in);
    3. int n = scanner.nextInt();
    4. int count = 0;
    5. for (int i = 0; i < 32; i++) {
    6. if (((n >> i) & 1) != 0) {
    7. count++;
    8. }
    9. }
    10. System.out.println(count);
    11. }

    缺陷:每个数都要按位与完32位,存在重复比较的情况,增加一个判断条件即可

    1. /**
    2. * 移动的过程中判断 n 是否为 0
    3. * 无循环判断32次,所以使用无符号右移
    4. * @param args
    5. */
    6. public static void main5(String[] args) {
    7. Scanner scanner = new Scanner(System.in);
    8. int n = scanner.nextInt();
    9. int count = 0;
    10. while(n!=0){
    11. if((n&1) != 0){
    12. count++;
    13. }
    14. n = n >>> 1;
    15. }
    16. System.out.println(count);
    17. }
    18. }

    此种情况下,避免了移动一定位数后n的左侧全为0的情况

    方法二:

    思路:相邻的两个数按位与运算,每相与一次即减少一个一,得到的即为大数中1的个数

    10= 0000 1010   &       9 = 0000 1001    -->  0000 1000     count =1

     0000 1000 & 0000 0111 = 0000 0000                                count = 2

    1. /**
    2. * n与n-1相与,每相与一次即少一个一,次数即n中1的个数
    3. */
    4. public static void main4(String[] args) {
    5. Scanner scanner = new Scanner(System.in);
    6. int n = scanner.nextInt();
    7. int count = 0;
    8. while (n != 0) {
    9. n = n&(n-1);
    10. count++;
    11. }
    12. System.out.println(count);
    13. }

    题目二:

    模拟登录

    编写代码模拟三次密码输入的场景。 最多能输入三次密码,密码正确,提示“登录成功”,密码错误, 可以重新输 入,最多输入三次。三次均错,则提示退出程序

    1. public static void main(String[] args) {
    2. Scanner s =new Scanner(System.in);
    3. int count = 3;
    4. while(count != 0){
    5. System.out.println("请输入你的密码,还有"+count+"次机会");
    6. String pass = s.nextLine();
    7. if(pass.equals("123")) {
    8. System.out.println("登录成功");
    9. break;
    10. }else{
    11. System.out.println("登录失败");
    12. count--;
    13. }
    14. }
    15. }

    题目三:

    二进制序列

    获取一个数二进制序列中所有的偶数位和奇数位, 分别输出二进制序列

    思路:提取所有的奇数位和偶数位,分别与1相与,结果为1则1,为0则0

    1. /**
    2. * 获取一个数二进制序列中所有的偶数位和奇数位, 分别输出二进制序列
    3. */
    4. public static void main(String[] args) {
    5. Scanner s = new Scanner(System.in);
    6. int n =s .nextInt();
    7. for (int i = 31; i >= 1 ; i-=2) {
    8. System.out.print(((n>>i)&1)+" ");
    9. }
    10. System.out.println();
    11. for (int i = 30; i >= 0 ; i-=2) {
    12. System.out.print(((n>>i)&1)+" ");
    13. }
    14. }

     题目四

    奇数位于偶数之前

    调整数组顺序使得奇数位于偶数之前。调整之后,不关心大小顺序。如数组:[1,2,3,4,5,6],调整后可能是:[1, 5, 3, 4, 2, 6]

    思路:保持 i  < j 判断奇偶,交换位置,使奇数位于偶数之前

    1. public static void func2(int[] array){
    2. int i= 0;
    3. int j =array.length-1;
    4. while(i
    5. while(i2 != 0){
    6. i++;
    7. }
    8. while(i2 == 0){
    9. j--;
    10. }
    11. int tmp = array[i];
    12. array[i] = array[j];
    13. array[j] =tmp;
    14. }
    15. }

    题目五

    两数之和

    给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target  的那 两个 整数,并返回它们的数组下标。

    思路:暴力解法,使用双层循环解题

    1. class Solution {
    2. public int[] twoSum(int[] nums, int target) {
    3. int[] nums2 =new int[2];
    4. for (int i = 0; i < nums.length; i++) {
    5. for (int j = i+1; j < nums.length && j!=i ; j++) {
    6. if (nums[i] + nums[j] == target) {
    7. nums2[0]=i;
    8. nums2[1]=j;
    9. }
    10. }
    11. }
    12. return nums2;
    13. }
    14. }

       💖如果文章对你有帮助,请多多点赞、收藏、评论、关注支持!!💖   

    ced485cbb11e458d81a746890b32cf3f.gif

  • 相关阅读:
    软件系统开发|公众号都有哪些展现形式?
    项目部署到Linux
    一篇博客学会系列(1) —— C语言中所有字符串函数以及内存函数的使用和注意事项
    认识Java笔记(2)
    【全开源】知识库文档系统(ThinkPHP+FastAdmin)
    新版本的AndroidStudio生产签名文件打包失败
    IOS OpenGL ES GPUImage 图像缩放 GPUImageTransformFilter
    vue+echarts ① echarts在vue中的使用
    【JVM技术专题】 深入学习JIT编译器实现机制「 原理篇」
    【leaflet】学习笔记1-4
  • 原文地址:https://blog.csdn.net/m0_56361048/article/details/126217175