java學習之電影《社交網路》Facemash演算法實現

來源:互聯網
上載者:User

標籤:

演算法理論參考:http://www.biaodianfu.com/fackbook-facemash-algorithm.html

實現:

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類的部分用法,如插入等操作;

        擴充:http://blog.sina.com.cn/s/blog_68f262210100mape.html

      http://jingyan.baidu.com/article/851fbc37d844553e1f15abe1.html

2 import java.lang.Math;

        熟悉Math類下指數運算;

        擴充:http://wxb-j2ee.iteye.com/blog/1010258

      http://blog.csdn.net/lixiang0522/article/details/7557851

      http://www.cnblogs.com/ksuifeng/archive/2010/03/24/1693856.html

3 import java.util.Comparator;

 import java.util.Collections;

        熟悉Comparator與Collections類,結合ArrayList類,實現數組的排序;

        擴充:http://www.blogjava.net/zygcs/archive/2008/01/17/176032.html

      http://www.cnblogs.com/fzzl/archive/2010/08/14/1799408.html

      http://blog.csdn.net/toweryangtao/article/details/7518325

 

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

          http://www.baidu.com/s?wd=myeclipse字型大小設定&tn=11000002_ie_dg

java學習之電影《社交網路》Facemash演算法實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.