算法理论参考: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