(8421.BCD code) weighted combination application

Source: Internet
Author: User
Tags strlen
1. Background analysis:A problem encountered in the actual development. (Member Level: Platinum, gold, silver, copper) There is a task list, you need to be based on different levels of membership to show their visible tasks (that is, gold can only see the gold medal visible tasks, their level see corresponding to your task). Generally how to solve it. Of course, the database on the task table to establish a field, indicating the task is which level can be seen not knot, so easy. 2. Question:So the question is, if I want to show it across the hierarchy. So that copper, gold visible, silver is not visible. That is to meet the various combinations of these four ... (Row 3 column combination formula C (n,m) so calculate again. haha 3. Methods:Method One: As mentioned above, build a new field varchar type, which is separated by commas, and which ranks are visible. such as "gold" or "gold, copper". (not convenient for select) method Two: Construct a new int field, use (8421 method) to distinguish weighted value.
4. Analysis:Method One benefit is intuitive, directly know which level is visible, the shortcoming is also obvious, I save 4 rank is to write 3 comma separate, and this is unfavorable to select query of AH. Method two only to create an int type of field, which stores a 1~15 two digits (and access to a large number of int type of query is not faster ~ ~) Then, see how the second method to solve the problem. 5. ProcessAssign weights to each member level as follows. 1: Bronze 2: Silver 4: Gold medal 8: Platinum 1. When creating a new task insert, such as I set platinum, bronze visible, by the top distribution, add weights 1+8 = 9, Database weight field LevelWill save 9. 2. For example, I am now a gold medal, how to let me see this data. First, I'm a gold medal. The corresponding level is 4, the 4 pass to the bottom function tolevelshow (4), returns an array that covers all possible combinations of gold medals. Returned as follows: (4,5,6,7,12,13,14,15). (4=4, Gold), (5=1+4, copper + Gold) (6=2+4, silver + gold) ... It is clear that copper + Platinum = 9 is no longer in this return value. 3. In select query, select * FROM table where ' Level ' in ([return value]), so that the task of having the gold medal involved is queried. 6. Conclusion So, this 8421 weight discrimination is essentially a statistical calculation of the corresponding 1248 combinations of all incoming values, and then the query where in these right inside the line. (well, how does he find the rank of the hierarchy?) This is the key core. The essence of the 2 into the number of a method, detailed can be viewed under the study of the function. ) This set of methods is not limited to the membership level, all the scenarios that meet this combination of circumstances can be used.
The function is as follows:
    /**
     * Input (1,2,4,8) One of the values that returns its 1248 combination of all combinations
     * @param input, (1,2,4,8) any number
     * @return The result
     of the addition of all its combinations * For example, input 8, return array (8,9,10,11,12,13,14,15),
     * because 8; 8+1=9; 8+2=10; 8+4=12; 8+1+2=11; 8+1+4=13; 8+2+4=14; 8+4+2+1=15
  * 15 is certain of these four numbers, because 8+4+2+1=15, the weight maximum
    /function tolevelshow ($level _show) {
        $ret = array ();
        Convert the level to binary
        $bin = Decbin ($level _show);
        $len = strlen ($bin);
        For ($i =15 $i >=1; $i-) {
            $tmp = Decbin ($i);
            $tmp = Str_pad ($tmp, 4, ' 0 ', str_pad_left);
            $tmp 1 = substr ($tmp,-$len, 1);
            if ($tmp 1==1) {
                $ret []= $i
            }}
        }
        return $ret;
    }

   /**
     * Split weights, view is by (1,2,4,8) These four numbers in the combination of the
     * @param input, 1-15 between the number, return 1248 combination.
     * @return 1248 Array
     * For example, enter 15, return array (1,2,4,8), because 1+2+4+8 = *
    function tolevel ($level _show) {
        $ret = Array ();
        Convert the level to binary 
        $bin = Decbin ($level _show);
        $len = strlen ($bin);
        for ($i =0; $i < $len; $i + +) {
            $tmp = $len-$i;
            if ($bin [$i]==1) {
                $dec = Bindec (Str_pad ($bin [$i], $tmp, ' 0 '));
                $ret []= $dec;
            }
        }
        return $ret;
    }



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.