给出分数数组,得到对应的名次数组
来源:互联网 发布:如何关闭淘宝推送广告 编辑:程序博客网 时间:2024/05/17 05:58
package test01;
/**
* 给出分数数组,得到对应的名次数组
* 列如有:score = {4,2,5,4}
* 则输出:rank = {2,3,1,2}
*/
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class ScoreRank {
// 输出数组
public static void show(int[] s){
for(int x:s) System.out.print(x);
System.out.println();
}
// 取得名次
public static int[] scoreRank(int[] score) {
int[] temp = new int[score.length];
List lis = new ArrayList();
for(int x:score) // 添加元素(不重复)
if(!lis.contains(x)) lis.add(x);
Collections.sort(lis);// 从小到大排序
Collections.reverse(lis);// 从大到小排序
for(int i=0;i<score.length;i++) // 下标从 0 开始
temp[i] = lis.indexOf(score[i])+1;// 所以:正常名次 = 取得下标 + 1
return temp;
}
public static void main(String[] args){
int[] score = {4,2,5,4};// 名次 {2,3,1,2}
int[] rank = scoreRank(score);// 取得名次
System.out.print("原始分数:");show(score);
System.out.print("对应名次:");show(rank);
}
}
/**
* 给出分数数组,得到对应的名次数组
* 列如有:score = {4,2,5,4}
* 则输出:rank = {2,3,1,2}
*/
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class ScoreRank {
// 输出数组
public static void show(int[] s){
for(int x:s) System.out.print(x);
System.out.println();
}
// 取得名次
public static int[] scoreRank(int[] score) {
int[] temp = new int[score.length];
List lis = new ArrayList();
for(int x:score) // 添加元素(不重复)
if(!lis.contains(x)) lis.add(x);
Collections.sort(lis);// 从小到大排序
Collections.reverse(lis);// 从大到小排序
for(int i=0;i<score.length;i++) // 下标从 0 开始
temp[i] = lis.indexOf(score[i])+1;// 所以:正常名次 = 取得下标 + 1
return temp;
}
public static void main(String[] args){
int[] score = {4,2,5,4};// 名次 {2,3,1,2}
int[] rank = scoreRank(score);// 取得名次
System.out.print("原始分数:");show(score);
System.out.print("对应名次:");show(rank);
}
}
0 0
- 给出分数数组,得到对应的名次数组
- 给出分数数组,得到对应的名次数组
- 给出分数数组,得到对应的名次数组,两种情况
- php 二维数组 知道keyvalue 怎么得到对应的value
- 36 输出比赛名次的数组result
- 给出a的定义 -- 指针 和 数组
- 给定只包含正数的数组,给出一个方法,将数组中的数拼接起来,得到的数,是最大的。
- 用函数初始化二维数组得到平年和闰年对应的天数
- 数组个元素名次计算
- 给定一个单调递增有序数组A,给定一个数字N,试给出一个算法得到A中该数字N出现的次数。
- 给出一个数组,打印出这个数组的所有的子数组
- 九数组分数
- 九数组分数
- 1005-九数组分数
- 九数组分数
- 九数组分数
- 九数组分数
- 九数组分数
- windows下使用MinGW+msys编译ffmpeg
- 启动Scrapy
- (30 hackdays day 4) PubNub – Connect everything in REALTIME way
- MySQL自学篇(十)——日期函数
- (30 hackdays day 7) Iron.io + beantalk - 来颗铁豆
- 给出分数数组,得到对应的名次数组
- (爱加密系列教程二十一)Android开发之初识Camera图像采集
- (爱加密系列教程二十三)伪加密+设备管理器不可删+webview漏洞
- QQ邮箱发送验证
- 实习篇---第三十一天
- Linux下删除出了某个文件之外的其他文件
- uva 11029 Leading and Trailing(求n^k的前几位和后几位 证明)
- 10 分钟为你的 App 加入实时通讯功能
- 10款最好的 Bootstrap 3.0 免费主题和模板