java投票排名实现
发布网友
发布时间:2022-05-05 08:13
我来回答
共4个回答
热心网友
时间:2023-10-23 22:18
两种实现方式:1)sql,2)java
1)sql语句-mysql
SELECT NAME,num,ranking
FROM (SELECT NAME,num,
IF (num =@num ,@ranking ,@ranking :=@ranking + 1)AS ranking,
@num := num
FROM score,
(SELECT @ranking := 0,@num:=-1) tmp
ORDER BY num DESC
)tmp
2) java实现
public static void main(String[] args) {
//初始化数据
List<Rank> ranks = Arrays.asList(new Rank("薛之谦", 100l),
new Rank("陈奕迅", 102l),
new Rank("田馥甄", 100l),
new Rank("周杰伦", 99l));
//按分数降序排序
ranks.sort((r1, r2) -> r2.getNum().compareTo(r1.getNum()));
//计算排名
int ranking = 0;
long num = -1;
for (Rank rank : ranks) {
rank.setRanking(rank.getNum() == num ? ranking : ++ranking);
num = rank.getNum();
}
//输出
ranks.forEach(o -> System.out.println(o.getName() + ";" + o.getNum() + ":" + o.getRanking()));
}
热心网友
时间:2023-10-23 22:18
//这个例子不关心从数据库读取数据,只关心根据排名打印结果.
import java.util.*;
public class Rank {
public void printRanking(Map<String,Integer> rm){
String[] ks=rm.keySet().toArray(new String[rm.size()]);
int[] rnk = new int[ks.length];
for(int i=0; i<ks.length; i++){
rnk[i] = rm.get(ks[i]);
}
int c = 0;
for(int i=0; i<ks.length-1; i++){
if(++c>1000)System.exit(0);
if(rnk[i]<rnk[i+1]){
String tmp = ks[i];
ks[i]=ks[i+1];
ks[i+1]=tmp;
rnk[i]+=rnk[i+1];
rnk[i+1]=rnk[i]-rnk[i+1];
rnk[i]-=rnk[i+1];
i-=i==0?1:2;
}
}
int tmp=1;
for(int i=0; i<ks.length; i++){
if(i==0){
System.out.print("第 1 名: "+ks[i]+"\t得票:"+rnk[i]);
}
else{
if(rnk[i]==rnk[i-1]){
System.out.print(", "+ks[i]+"\t得票:"+rnk[i]);
}
else{
System.out.print("\n第 "+(++tmp)+" 名: "+ks[i]+"\t得票:"+rnk[i]);
}
}
// if(tmp==3)break;//只打印前3名
}
System.out.println();
}
public static void main(String[] args){
//MAP里保存歌手名和对应的投票数,这个就直接从数据库里读取,稍加处理就行.
Map<String,Integer> rm = new HashMap<String,Integer>();
rm.put("王菲",136);
rm.put("周华健",61);
rm.put("S.H.E",76);
rm.put("刘德华",81);
rm.put("林XY",86);
rm.put("朱ZZ",61);
rm.put("阿牛",86);
rm.put("张UU",62);
rm.put("金莎",36);
rm.put("邓丽君",86);
Rank r = new Rank();
r.printRanking(rm);
}
}
热心网友
时间:2023-10-23 22:19
select * from ... order by num
热心网友
时间:2023-10-23 22:19
rm.put("阿牛",86);
热心网友
时间:2023-10-23 22:18
两种实现方式:1)sql,2)java
1)sql语句-mysql
SELECT NAME,num,ranking
FROM (SELECT NAME,num,
IF (num =@num ,@ranking ,@ranking :=@ranking + 1)AS ranking,
@num := num
FROM score,
(SELECT @ranking := 0,@num:=-1) tmp
ORDER BY num DESC
)tmp
2) java实现
public static void main(String[] args) {
//初始化数据
List<Rank> ranks = Arrays.asList(new Rank("薛之谦", 100l),
new Rank("陈奕迅", 102l),
new Rank("田馥甄", 100l),
new Rank("周杰伦", 99l));
//按分数降序排序
ranks.sort((r1, r2) -> r2.getNum().compareTo(r1.getNum()));
//计算排名
int ranking = 0;
long num = -1;
for (Rank rank : ranks) {
rank.setRanking(rank.getNum() == num ? ranking : ++ranking);
num = rank.getNum();
}
//输出
ranks.forEach(o -> System.out.println(o.getName() + ";" + o.getNum() + ":" + o.getRanking()));
}
热心网友
时间:2023-10-23 22:18
//这个例子不关心从数据库读取数据,只关心根据排名打印结果.
import java.util.*;
public class Rank {
public void printRanking(Map<String,Integer> rm){
String[] ks=rm.keySet().toArray(new String[rm.size()]);
int[] rnk = new int[ks.length];
for(int i=0; i<ks.length; i++){
rnk[i] = rm.get(ks[i]);
}
int c = 0;
for(int i=0; i<ks.length-1; i++){
if(++c>1000)System.exit(0);
if(rnk[i]<rnk[i+1]){
String tmp = ks[i];
ks[i]=ks[i+1];
ks[i+1]=tmp;
rnk[i]+=rnk[i+1];
rnk[i+1]=rnk[i]-rnk[i+1];
rnk[i]-=rnk[i+1];
i-=i==0?1:2;
}
}
int tmp=1;
for(int i=0; i<ks.length; i++){
if(i==0){
System.out.print("第 1 名: "+ks[i]+"\t得票:"+rnk[i]);
}
else{
if(rnk[i]==rnk[i-1]){
System.out.print(", "+ks[i]+"\t得票:"+rnk[i]);
}
else{
System.out.print("\n第 "+(++tmp)+" 名: "+ks[i]+"\t得票:"+rnk[i]);
}
}
// if(tmp==3)break;//只打印前3名
}
System.out.println();
}
public static void main(String[] args){
//MAP里保存歌手名和对应的投票数,这个就直接从数据库里读取,稍加处理就行.
Map<String,Integer> rm = new HashMap<String,Integer>();
rm.put("王菲",136);
rm.put("周华健",61);
rm.put("S.H.E",76);
rm.put("刘德华",81);
rm.put("林XY",86);
rm.put("朱ZZ",61);
rm.put("阿牛",86);
rm.put("张UU",62);
rm.put("金莎",36);
rm.put("邓丽君",86);
Rank r = new Rank();
r.printRanking(rm);
}
}
热心网友
时间:2023-10-23 22:19
select * from ... order by num
热心网友
时间:2023-10-23 22:19
rm.put("阿牛",86);