Java Learning movie "Social network" Facemash algorithm implementation

Source: Internet
Author: User
Tags array sort

Algorithmic theory Reference: http://www.biaodianfu.com/fackbook-facemash-algorithm.html

Realize:

Import java.util.List;

public class User {
private String name; User name
private double integral; Integral
private double rank; Ranking

Public User (String name,double integral,int rank) {
THIS.name = name;
This.integral = integral;
This.rank = rank;
}
Public User () {

}
/**
* Get user points ranking
* @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;
}

/**
* Get user name points rank
* @return
*/
Public String getuserstring ()
{
return getrankstring () + ":" +getnamestring () + "" +
"" +getintegralstring ();
}
/**
* Print user Information
* @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;


/**
* Get the results of the game
* @return
*/
Public double Getscore ()
{
if (score = = 0)
{
Score = 1;
}
Else
{
Score = 0;
}
return score;
}


public static long Pow (double x,double y)
/**
* Get success expectations
* @return
*/
Public double getexpect_a ()
{
Double result = 0;
1/(1+10^ ((Ib-ia)/400))
result = 1/(1 + math.pow (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 (user_a.getintegral ()-user_b.getintegral ())/400);
return result;
}

/**
* Earn points after the game
* @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;
}
}

Learning experience:

1 Import java.util.List;

Import java.util.ArrayList;

Familiar with partial usage of list and ArrayList class, such as inserting operations;

Extension: http://blog.sina.com.cn/s/blog_68f262210100mape.html

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

2 Import Java.lang.Math;

Be familiar with the arithmetic of math class;

Extension: 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;

familiar with comparator and collections class, combine ArrayList class, realize array sort;

Extension: 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 Fix the problem:

Java hint cannot invoke compareTo (double) on the primitive type double

Reason:


Workaround: Set the Getintegral method of the object to double instead of double;

Public Double getintegral () {
return integral;
}

5 MyEclipse

Http://www.baidu.com/s?wd=myeclipse Font Size setting &TN=11000002_IE_DG

Java Learning movie "Social network" Facemash algorithm implementation

Related Article

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.