获取一个积分集合。怎么样取到其中相同的成绩,并且让他们成绩相同的元素,名次一样。求实现方法
要是按你字面说的,我觉得直接排序就行了
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
int[] scores = { 10, 9, 9, 8, 6, 5, 5, 5, 4, 1 };
int prescore = scores[0];
int currpos = 1;
int same = 0;
for (int i : scores)
{
if (i == prescore)
{
same++;
}
else
{
curpos += (same + 1);
same = 0;
prescore = i;
}
System.out.println(i + "名次" + curpos);
}
}
}
public class jia {
public static void main(String[] args) {
int [] a ={1,7,3,3,9,6,6,6,4};
int temp;
for(int i=0;i<a.length-1;i++){
for(int j=i+1;j<a.length;j++){
if(a[j]>a[i]){
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
temp = 1;
System.out.println(a[0]+":"+temp);
for(int i=1;i<a.length;i++){
if(a[i]==a[i-1]){
System.out.println(a[i]+":"+temp);
}else{
System.out.println(a[i]+":"+(++temp));
}
}
}
}
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
int[] scores = { 10, 9, 9, 8, 6, 5, 5, 5, 4, 1 };
int prescore = -1;
int cur = 0;
int same = 0;
for (int i : scores)
{
if (i == prescore)
{
System.out.println(i + " - " + cur);
same++;
}
else
{
cur = cur + same;
cur++;
prescore = i;
same = 0;
System.out.println(i + " - " + cur);
}
}
}
}
10 - 1
9 - 2
9 - 2
8 - 4
6 - 5
5 - 6
5 - 6
5 - 6
4 - 9
1 - 10
public static void main(String[] args){
int[] goal={9,9,6,4,5,4,4,11,23,1,21,45,65,78,98,45,2,46,21,32,12,45,65,78,98,7,7,7,4,13,13,15,4,88,44,8};
int[] mark=new int[99];
int m=0;
for (int i=0;i mark[goal[i]]++;
}
List result=new ArrayList();
List times=new ArrayList<>();
for (int i=1;i<mark.length;i++){
if(mark[i]!=0){
result.add(i);
times.add(mark[i]);
}
}
for(int i=0;i<result.size();i++){
System.out.println("第"+(i+1)+"名,积分为:"+result.get(i)+"——————有"+times.get(i)+"个数据");
}
}
这个行了应该
public static void main(String[] args){
int[] goal={9,9,6,4,5,4,4,11,23,1,21,45,65,78,98,45,2,46,21,32,12,45,65,78,98,7,7,7,4,13,13,15,4,88,44,8};
int[] mark=new int[99];
int m=0;
for (int i=0;i<goal.length;i++){
mark[goal[i]]++;
}
List<Integer> result=new ArrayList();
List<Integer> times=new ArrayList<>();
for (int i=1;i<mark.length;i++){
if(mark[i]!=0){
result.add(i);
times.add(mark[i]);
}
}
for(int i=0;i<result.size();i++){
System.out.println("第"+(i+1)+"名,积分为:"+result.get(i)+"——————有"+times.get(i)+"个数据");
}
}
这个类似计数排序,去了重复数据