• 签到功能二进制实现


    思路

    一个月最多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));
        }
    }
  • 相关阅读:
    in_array()和explode()的使用笔记
    写sql语句连接的时候注意的一个小细节
    在thinkphp框架模板中引用session
    查询数据库所有(某个)表中字段名,数据类型,说明等,导出数据字典
    (委托事件处理)关于多线程执行显示进度条的实例(转)&&线程间操作无效: 从不是创建控件“rtxtEntryNO”的线程访问它。
    判断当前线程所处的状态 (转)以及终止当前线程
    string 字符串的分隔处理与list的相互转换
    C# 动态调用webservice
    C#中的List<string>泛型类示例
    命名空间"system.web"中不存在类型或命名空间名称security"
  • 原文地址:https://www.cnblogs.com/xiaomaoyvtou/p/12924056.html
Copyright © 2020-2023  润新知