zoukankan      html  css  js  c++  java
  • 签到功能二进制实现

    思路

    一个月最多31天,四个字节有32位,每个bit位代表一天,签到则置为1,默认是0代表没签到

    例:

    import java.util.Calendar;
    
    /**
     * 签到工具类
     */
    public class SigningManager {
    
        private int element;
    
        public SigningManager() {
            this.element = 0;
        }
    
        /**
         * 当天签到
         *
         * @return 签到成功true, 失败false
         */
        public boolean signToday() {
            int dayOfMonth = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
            int old = this.element;
            element |= 1 << (dayOfMonth - 1);
            return element != old;
        }
    
        public boolean signToday(int day) {
            int old = this.element;
            element |= 1 << (day - 1);
            return element != old;
        }
    
        /**
         * 查询当月某天是否签到
         *
         * @param dayOfMonth 当月第几天
         * @return 签到返回true, 否则false
         */
        public boolean getSignStatus(int dayOfMonth) {
            return (element & (1 << (dayOfMonth - 1))) != 0;
        }
    
        /**
         * @return 当月签到次数
         */
        public int getSignDays() {
            return Integer.bitCount(element);
        }
    
        /**
         * 统计连续签到天数的次数
         *
         * @param day
         * @return
         */
        public int continuousNumber(int day) {
            String str = intToBinary32(element, 32);
            int count = 0;
            int num = 0;
            for (int i = 0; i < str.length(); i++) {
                if (str.charAt(i) == '1') {
                    num++;
                } else {
                    num = 0;
                }
                if (num == day) {
                    count++;
                    num = 0;
                }
            }
            return count;
        }
    
        public static String intToBinary32(int i, int bitNum) {
            StringBuilder binaryStr = new StringBuilder(Integer.toBinaryString(i));
            while (binaryStr.length() < bitNum) {
                binaryStr.insert(0, "0");
            }
            return binaryStr.toString();
        }
    
        public static void main(String[] args) {
    //        //A=26
    //        //0000 0000 0000 0000 0000 0000 0001 1010
    //        //00000000000000000000000000011010
    //
    //        SigningManager signingManager = new SigningManager();
    //        signingManager.element = 26;
    //
    //        System.out.println(intToBinary32(signingManager.element, 32));
    //
    //        System.out.println(intToBinary32(1 << (1 - 1), 32));
    //        System.out.println(intToBinary32(1 << (2 - 1), 32));
    //        System.out.println(intToBinary32(1 << (3 - 1), 32));
    //        System.out.println(intToBinary32(1 << (4 - 1), 32));
    //        System.out.println(intToBinary32(1 << (5 - 1), 32));
    //
    //        System.out.println("------------------------------------------");
    //
    //        System.out.println(Integer.parseInt("00000000000000000000000000000001", 2));
    //        System.out.println(Integer.parseInt("00000000000000000000000000000010", 2));
    //        System.out.println(Integer.parseInt("00000000000000000000000000000100", 2));
    //        System.out.println(Integer.parseInt("00000000000000000000000000001000", 2));
    //        System.out.println(Integer.parseInt("00000000000000000000000000010000", 2));
    //
    //        System.out.println("------------------------------------------");
    //
    //        System.out.println(signingManager.element & 1);
    //        System.out.println(signingManager.element & 2);
    //        System.out.println(signingManager.element & 4);
    //        System.out.println(signingManager.element & 8);
    //        System.out.println(signingManager.element & 16);
    //
    //        System.out.println("------------------------------------------");
    //
    //        System.out.println((signingManager.element & 1) != 0);
    //        System.out.println((signingManager.element & 2) != 0);
    //        System.out.println((signingManager.element & 4) != 0);
    //        System.out.println((signingManager.element & 8) != 0);
    //        System.out.println((signingManager.element & 16) != 0);
    //
    //        //00000000000000000000000000011010 &
    //        //00000000000000000000000000000001
    //        //00000000000000000000000000000000
    //
    //        //00000000000000000000000000011010 &
    //        //00000000000000000000000000000010
    //        //00000000000000000000000000000010
    //
    //        //00000000000000000000000000011010 &
    //        //00000000000000000000000000010000
    //        //00000000000000000000000000010000
    //
    //        System.out.println("------------------------------------------");
    //
    //
    //        System.out.println(intToBinary32(1 << (20 - 1), 32));
    //        //00000000000000000000000000011010 |
    //        //00000000000010000000000000000000
    //        //00000000000010000000000000011010
    //        System.out.println(Integer.parseInt("00000000000010000000000000011010", 2));
    //        System.out.println(signingManager.element |= (1 << (20 - 1)));
    //
    //        System.out.println("------------------------------------------");
    //        for (int i = 1; i <= 31; i++) {
    //            System.out.println(i + "    " + signingManager.getSignStatus(i));
    //        }
    
            //例:5天签到连续送物品
            // 31 0x1f 11111
            SigningManager signingManager = new SigningManager();
            signingManager.signToday(1);
            signingManager.signToday(2);
            signingManager.signToday(3);
            signingManager.signToday(4);
    //        signingManager.signToday(5);
            signingManager.signToday(6);
            signingManager.signToday(7);
            signingManager.signToday(8);
            signingManager.signToday(9);
    //        signingManager.signToday(10);
            signingManager.signToday(11);
            signingManager.signToday(12);
            signingManager.signToday(13);
            signingManager.signToday(14);
    //        signingManager.signToday(15);
    //        signingManager.signToday(16);
    //        signingManager.signToday(17);
    //        signingManager.signToday(18);
    //        signingManager.signToday(19);
    //        signingManager.signToday(20);
    //        signingManager.signToday(21);
    //        signingManager.signToday(22);
    //        signingManager.signToday(23);
    //        signingManager.signToday(24);
    //        signingManager.signToday(25);
    //        signingManager.signToday(26);
    //        signingManager.signToday(27);
    //        signingManager.signToday(28);
    //        signingManager.signToday(29);
    //        signingManager.signToday(30);
    //        signingManager.signToday(31);
    //        signingManager.signToday(32);
    
            System.out.println(signingManager.element);
            System.out.println(intToBinary32(signingManager.element, 32));
    
            //补签
            signingManager.signToday(10);
            System.out.println(intToBinary32(signingManager.element, 32));
            System.out.println(intToBinary32(31, 32));
    
            System.out.println(signingManager.continuousNumber(2));
        }
    }
  • 相关阅读:
    手写Linq To Object
    4、IOC--内置Unity容器的使用
    WebApi 基于JWT实现Token签名认证
    软件架构师之路--观察者模式
    EF--封装三层架构IOC
    EF--主外键关系导航属性
    EF--EntityState相互转换
    证明task线程是来源于线程池的,线程重用
    3、IOC--手写Unity容器--链式依赖--第N层依赖注入
    2、IOC--手写Unity容器--第一层依赖注入
  • 原文地址:https://www.cnblogs.com/xiaomaoyvtou/p/12924056.html
Copyright © 2011-2022 走看看