学生分数排序

输入是个学生分数,按座位或者名字之类的随机,输出是按照输入顺序然后后面加上排名

 1 // C_Study.cpp : 定义控制台应用程序的入口点。
 2 //
 3 
 4 #include "stdafx.h"
 5 #include <iostream>
 6 
 7 using namespace std;
 8 
 9 #define MAX 100
10 #define MIN 0
11 
12 
13 
14 int _tmain(int argc, _TCHAR* argv[])
15 {
16     int score[MAX+1]={0};
17     int juni[MAX+2]={0};
18 
19     int count=0,i,f=0;
20 
21     do{
22 
23         printf("输入分数,-1结束:");
24         scanf("%d",&score[count++]);  
25     
26     }while(score[count-1]!=-1);
27     count--;
28 
29     for(i=0;i<count;i++)
30         juni[score[i]]++;
31     juni[MAX+1]=1;
32 
33     for(i=MAX;i>=MIN;i--)
34                          juni[i]=juni[i]+juni[i+1]; 
35 
36     
37     printf("得分排行
");
38     
39     for(i=0;i<count;i++)
40         printf("%d %d
",score[i],juni[score[i]+1]);
41 
42 
43     while(1)
44     {
45         
46     }
47     return 0;
48 }
main

出来是分数名次:

看了半天才看明白:

 for(i=0;i<count;i++)
  juni[score[i]]++;
 juni[MAX+1]=1;

在0-100之间,哪几个分值被录入了,就进行记录,有几个学生就记录几次,没有此分值的就为0,比如上面6被录入两次则此时juni[6]=2

 for(i=MAX;i>=MIN;i--)
  juni[i]=juni[i]+juni[i+1];

从上到下进行一遍遍历,因为juni[MAX+1]=1;所以如果MAX分数录入,执行后juni[MAX]=1,有录入则juni[MAX]比之前+1,这样其实每个分数的排名都错了一位,这样可以计算出并列排名情况,如果输入多次就根据之前记录的+n,

juni[MAX+1]=1就是为了把第一名位置设置为1,即使有n个人都为MAX分数排名也都是1,第二个分值的人才会去递增。

所以最后输出时候:printf("%d %d ",score[i],juni[score[i] +1]);

如果我们想输出时候按分数高低顺序的话,再加俩数据进行记录即可,这时候juni[i]=juni[i]+juni[i+1];可以不用了,具体如下

 1 // C_Study.cpp : 定义控制台应用程序的入口点。
 2 //
 3 
 4 #include "stdafx.h"
 5 #include <iostream>
 6 
 7 using namespace std;
 8 
 9 #define MAX 100
10 #define MIN 0
11 
12 
13 
14 int _tmain(int argc, _TCHAR* argv[])
15 {
16     int score[MAX+1]={0};
17     int juni[MAX+2]={0};
18     int f_score[MAX+1]={0};
19     int f_juni[MAX+1]={0};
20 
21     int count=0,i,f=0,mingci=1,m;
22 
23     do{
24 
25         printf("输入分数,-1结束:");
26         scanf("%d",&score[count++]);  
27     
28     }while(score[count-1]!=-1);
29     count--;
30 
31     for(i=0;i<count;i++)
32         juni[score[i]]++;
33     juni[MAX+1]=1;
34 
35     for(i=MAX;i>=MIN;i--)
36     {
37         if(juni[i]>0)
38         {
39             for(m=0;m<juni[i];m++)
40             {
41               f_score[f]=i;
42               f_juni[f]=mingci;
43               f++;
44             }
45         
46             mingci+=juni[i];            
47         }
48         juni[i]=juni[i]+juni[i+1];
49             
50     }
51 
52     for(i=0;i<count;i++)
53        printf("%d %d
",f_score[i],f_juni[i]);
54     
55     printf("得分排行
");
56     
57     for(i=0;i<count;i++)
58         printf("%d %d
",score[i],juni[score[i]+1]);
59 
60 
61     while(1)
62     {
63         
64     }
65     return 0;
66 }
View Code

改动:
 int f_score[MAX+1]={0};
 int f_juni[MAX+1]={0};

int count=0,i,f=0,mingci=1,m;

 for(i=MAX;i>=MIN;i--)
 {
  if(juni[i]>0)
  {
      for(m=0;m<juni[i];m++)
   {
     f_score[f]=i;
     f_juni[f]=mingci;
     f++;
   }
  
   mingci+=juni[i];   
  }
  juni[i]=juni[i]+juni[i+1];
   
 }

 

原文地址:https://www.cnblogs.com/wwjdwy/p/3388068.html