“红音列表实现引发的思考”的版本间的差异

来自姬鸿昌的知识库
跳到导航 跳到搜索
第24行: 第24行:
 
要求实现一套权重机制
 
要求实现一套权重机制
  
 +
简单的实现是:<syntaxhighlight lang="java">
 +
public class RankRecord {
  
int 4个字节,32位,如果每个属性的有效值只有0、1那么,
+
    /**
 +
    * 性别
 +
    */
 +
    private int gender;
  
假如
+
    /**
 +
    * 是否有房
 +
    */
 +
    private int house;
 +
 
 +
    /**
 +
    * 是否有车
 +
    */
 +
    private int car;
 +
 
 +
    /**
 +
    * 身高
 +
    */
 +
    private int height;
 +
 
 +
    /**
 +
    * 体重
 +
    */
 +
    private int weight;
 +
 
 +
    /**
 +
    * 权重
 +
    */
 +
    private int weights;
 +
 
 +
    public RankRecord(int gender, int house, int car, int height, int weight) {
 +
        this.gender = gender;
 +
        this.house = house;
 +
        this.car = car;
 +
        this.height = height;
 +
        this.weight = weight;
 +
        initWeights();
 +
    }
 +
 
 +
    /**
 +
    * 初始化权重
 +
    */
 +
    private void initWeights() {
 +
        int weights = gender;
 +
        weights = (weights << 1) + house;
 +
        weights = (weights << 1) + car;
 +
        weights = (weights << 1) + height;
 +
        weights = (weights << 1) + weights;
 +
        this.weights = weights;
 +
    }
 +
 
 +
}
 +
</syntaxhighlight>

2022年11月10日 (四) 14:47的版本

需求是:

要为当前登录用户显示一个列表,这个列表里是过去72小时内所有登录过的用户;

这些用户要按一定的规则排序,规则是:

比如当前登录用户是女性,那么男性用户要排在女性用户前面(1、0);

同样是男性用户,有房的要排在没房的前面(1、0);

同样有房的,有车的要排在没车的前面(1、0);

受教育程度(博士、硕士、学士、高中)……(4、3、2、1);

婚姻状况(未婚、离异、丧偶)(3、2、1)

工作(央企、国企、公务员、……)(4、3、2、1)

年龄(28~35、35~40、23~28、……)(4、3、2、1);

身材((kg~kg)苗条、(kg~kg)匀称、(kg~kg)偏瘦、(kg~kg)偏胖、……)(4、3、2、1)


要求实现一套权重机制

简单的实现是:

public class RankRecord {

    /**
     * 性别
     */
    private int gender;

    /**
     * 是否有房
     */
    private int house;

    /**
     * 是否有车
     */
    private int car;

    /**
     * 身高
     */
    private int height;

    /**
     * 体重
     */
    private int weight;

    /**
     * 权重
     */
    private int weights;

    public RankRecord(int gender, int house, int car, int height, int weight) {
        this.gender = gender;
        this.house = house;
        this.car = car;
        this.height = height;
        this.weight = weight;
        initWeights();
    }

    /**
     * 初始化权重
     */
    private void initWeights() {
        int weights = gender;
        weights = (weights << 1) + house;
        weights = (weights << 1) + car;
        weights = (weights << 1) + height;
        weights = (weights << 1) + weights;
        this.weights = weights;
    }

}