织梦CMS - 轻松建站从此开始!

欧博ABG-会员注册-官网网址

java学习之电影《社交网络》Facemash算法实现

时间:2024-05-21 06:37来源: 作者:admin 点击: 69 次
算法理论参考:http://www.biaodianfu.com/fackbook-facemash-algorithm.html实现:import java.util.List;public class User { private String name; //用户名 priva

算法理论参考:

实现:

import java.util.List;

public class User {
private String name; //用户名
private double integral; //积分
private double rank; //排名

public User(String name,double integral,int rank){
this.name = name;
this.integral = integral;
this.rank = rank;
}
public User(){

}
/**
* 得到用户积分排名
* @return
*/
public int getUserRanking(){
int result = 0;

return result;
}

public String getName() {
return name;
}
public String getNameString() {
return getName();
}

public Double getIntegral(){
return integral;
}
public double getIntegralString()
{
return getIntegral();
}
public void getNewIntegral(double a){
this.integral = a;
}

public double getRank(){
return rank;
}
public double getRankString()
{
return getRank();
}
public void getNewRank(double a){
this.rank = a;
}

/**
* 得到用户名 积分 排名次
* @return
*/
public String getUserString()
{
return getRankString()+": "+getNameString()+" "+
" "+getIntegralString();
}
/**
* 打印用户信息
* @return
*/

public static String getOutputUser(List User)
{
String result="";
for (int i = 0; i < User.size(); i++)
{
User item=(User)User.get(i);
result+=item.getUserString()+"\r\n";
}
return result;
}

}

import java.util.ArrayList;import java.util.Comparator;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;

public class UserItem {

public static void main(String[] args)
{
// TODO Auto-generated method stub
test1();
}

public static void test1()
{
User p=new User("WangLei",0.55,1);
User p2=new User("YangJiang",0.54,2);
User p3=new User("XuePeng",0.53,3);
User p4=new User("ZhuKai",0.52,4);
User p5=new User("ZhenShan",0.51,5);
List list=new ArrayList();
list.add(p);
list.add(p2);
list.add(p3);
list.add(p4);
list.add(p5);
System.out.println(User.getOutputUser(list));
match aa = new match();
aa.setUser(p,p2);
aa.setExpect();
aa.setScore();
p.getNewIntegral(aa.setIntegral_a());
p2.getNewIntegral(aa.setIntegral_b());
aa.setUser(p3,p4);
aa.setExpect();
aa.setScore();
p3.getNewIntegral(aa.setIntegral_a());
p4.getNewIntegral(aa.setIntegral_b());
aa.setUser(p3,p5);
aa.setExpect();
aa.setScore();
p3.getNewIntegral(aa.setIntegral_a());
p5.getNewIntegral(aa.setIntegral_b());
aa.setUser(p2,p4);
aa.setExpect();
aa.setScore();
p2.getNewIntegral(aa.setIntegral_a());
p4.getNewIntegral(aa.setIntegral_b());

System.out.println(User.getOutputUser(list));

comparatorUser comparator=new comparatorUser();
Collections.sort(list, comparator);

for (int i=0;i<list.size();i++){
User user_temp=(User)list.get(i);
user_temp.getNewRank(list.size()-i);
}
System.out.println(User.getOutputUser(list));
}

}

import java.lang.Math;

public class match {
private User user_a;
private User user_b;
private double expect_a = 0;
private double expect_b = 0;
private double score_a = 0;
private double score_b = 0;
private double score = 0;


/**
* 得到比赛结果
* @return
*/
public double getScore()
{
if(score == 0)
{
score = 1;
}
else
{
score = 0;
}
return score;
}


//public static long pow(double x,double y)
/**
* 得到成功期望
* @return
*/
public double getExpect_a()
{
double result = 0;
//1/(1+10^((Ib-Ia)/400))
result = 1/(1 + Math.pow(10, (user_b.getIntegral()-user_a.getIntegral())/400));
return result;
}
public double getExpect_b()
{
double result = 0;
//1/(1+10^((Ia-Ib)/400))
result = 1/(1 + Math.pow(10, (user_a.getIntegral()-user_b.getIntegral())/400));
return result;
}

/**
* 得到比赛后积分
* @return
*/
public double setIntegral_a()
{
//user_a.getIntegral() = user_a.getIntegral() + score_a - expect_a
return user_a.getIntegral() + score_a - expect_a;
}
public double setIntegral_b()
{
return user_b.getIntegral() + score_b - expect_b;
}
public void setUser(User a,User b)
{
this.user_a = a;
this.user_b = b;
}


public void setExpect()
{
this.expect_a = getExpect_a();
this.expect_b = getExpect_b();
}
public void setScore()
{
this.score_a = getScore();
this.score_b = getScore();
}

}

import java.util.Comparator;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;

public class comparatorUser implements Comparator{

public int compare(Object arg0, Object arg1) {
User user0=(User)arg0;
User user1=(User)arg1;
int flag =0;
flag = user0.getIntegral().compareTo(user1.getIntegral());
return flag;
}
}

学习心得:

1 import java.util.List;  

import java.util.ArrayList;

熟悉List与ArrayList类的部分用法,如插入等操作;

扩展:

      

2 import java.lang.Math;

熟悉Math类下指数运算;

扩展:

      

      

3 import java.util.Comparator;

 import java.util.Collections;

熟悉Comparator与Collections类,结合ArrayList类,实现数组的排序;

扩展:

      

      

4 解决问题:

    java 提示cannot invoke compareTo(double) on the primitive type double

      原因:

   错误代码缘由:flag = user0.getIntegral().compareTo(user1.getIntegral());
   解决办法: 把对象的getIntegral方法设为Double 而不是double;

    public Double getIntegral(){
    return integral;
    }

5 MyEclipse

          ?wd=myeclipse字体大小设置&tn=11000002_ie_dg

(责任编辑:)
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:
发布者资料
查看详细资料 发送留言 加为好友 用户等级: 注册时间:2024-12-22 17:12 最后登录:2024-12-22 17:12
栏目列表
推荐内容