HDU-2093考试排名

浪尽此生 提交于 2020-03-01 17:34:46

题目链接

HDU-2093

题意

实时评测系统根据提交时间和对错误次数罚时进行排名。

思路

  1. 结构体存储学生信息
  2. 采用字符串读入,遍历字符串,计算每道题的提交时间和错误次数,存入结构体。
  3. 计算所有同学的AC题目以及所用时间,一定要注意罚时的计算规则,没有AC的题目不计算罚时,对结构体进行排名。多要素排序可以重载<运算符
bool operator<(student& p) {
  if (scoresum != p.scoresum)
   return scoresum > p.scoresum;   //降序
  if (timesum != p.timesum)
   return timesum < p.timesum;   //升序
  return name < p.name;
 }
  1. 注意memset函数的使用方法,头文件<cstring>, 用法为void* memset( void* dest, int ch, std::size_t count ); 转换值 ch 为 unsigned char 并复制它到 dest 所指向对象的首 count 个字节。若该对象是潜在重叠的子对象或非可平凡复制 ,则行为未定义。若 count 大于 dest 所指向的对象大小,则行为未定义。
    可平凡复制的:(TriviallyCopyable) (例如标量、 C 兼容的结构体或可平凡复制类型的数组)

代码

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
#include<string.h>
using namespace std;
 

class student {

    public:

 

         string name;

         int* score;

         int* time;

         int num;

         int scoresum;

         int timesum;

         int fa;

 

         student(int number=0,int m=0) {

 

             score= new int[number + 1];

             time=new int[number + 1];

             memset(score,0, sizeof(score[number+1]));

             memset(time,0, sizeof(score[number + 1]));

             num= number;

             scoresum= 0;

             timesum= 0;

             fa= m;

         }

         void upset() {

             memset(score,'0', sizeof(score[num+1]));

             memset(time,'0', sizeof(score[num + 1]));

         }

         student(const student& b) { //深拷贝

             num= b.num;

             scoresum= b.scoresum;

             timesum= b.timesum;

             fa= b.fa;
             //  memset(name,'\0', sizeof(name[10]));

             score = new int[num + 1];

             time= new int[num + 1];

             memset(score,'0', sizeof(score[num + 1]));

             memset(time,'0', sizeof(score[num + 1]));

             //  for (int i = 0;i < 10; i++)

             //  {

             //       name[i] =b.name[i];

             //  }

             name= b.name;

             for (int i = 1; i <num+1; i++) {

                  score[i]= b.score[i];

                  time[i]= b.time[i];

             }

 

         }

         student operator=(const student& b) {

             student c;

 

             c.num= b.num;

             c.scoresum= b.scoresum;

             c.timesum= b.timesum;

             c.fa= b.fa;

             //  memset(c.name,'\0', sizeof(c.name[10]));

             c.score= new int[num + 1];

             c.time= new int[num + 1];

             memset(c.score,'0', sizeof(int)*(num+1));

             memset(c.time,'0', sizeof(int)*(num+1));

             //  for (int i = 0;i < 10; i++)

             //  {

             //       c.name[i] =b.name[i];

             //  }

             c.name= b.name;

             for (int i = 1; i < num +1; i++) {

                  c.score[i]= b.score[i];

                  c.time[i]= b.time[i];

             }

             return c;

         }

         void comulate() {

             for (int i = 1; i <= num;i++) {
                  if (score[i] != 0)scoresum++;

                  timesum=timesum + time[i] * fa + score[i];

             }

         }
         bool operator<(student& p) {
//       comulate();

//       p.comulate();
             if (scoresum != p.scoresum)

                  return scoresum > p.scoresum;

             if (timesum != p.timesum)

                  return timesum < p.timesum;

             return name < p.name;

         }

};

 

class arraylist {

         student* arr;

         int arraysize;

         int current;

    public:

         arraylist(int size = 50) {

             arraysize= size;

             current= 0;

             arr= new student[arraysize];

         }
         int size() {
             return current;
         }

         void push(student& element) {

             element.comulate();

             arr[current].name= element.name;

             arr[current].scoresum= element.scoresum;

             arr[current].timesum= element.timesum;

//       cout<< arr[current].timesum;

             current++;
 
             if (current >arraysize - 2) {

                  student* temp = new student[2 * arraysize];

                  for (int i = 0; i <current; i++) {

                      temp[i].name= arr[i].name;

                      temp[i].scoresum= arr[i].scoresum;

                      temp[i].timesum= arr[i].timesum;

                  }

                  arraysize*= 2;

                 delete[] arr;
                  arr= temp;
             }

         }

 

         void output() {

             //       cout<<"aaa";

//       sort(arr,arr + current*sizeof(student));

             for(int i=0; i<current;i++)

                  for(int j=i; j<current;j++) { //冒泡排序

                      if(j>0&&!(arr[j-1] <arr[j]))
{

                          string a1=arr[j].name;

                          int score1=arr[j].scoresum;

                          int time1=arr[j].timesum;

                          arr[j].name=arr[j-1].name;

                          arr[j].scoresum=arr[j-1].scoresum;

                          arr[j].timesum=arr[j-1].timesum;

                          arr[j-1].name=a1;

                          arr[j-1].scoresum=score1;

                           arr[j-1].timesum=time1;

                      }

                  }

 

             for (int i = 0; i <current; i++) {

                  char k[10];

                  strcpy(k,arr[i].name.c_str());

                  printf("%-10s ", k);

                  printf("%2d ", arr[i].scoresum);

                  printf("%4d", arr[i].timesum);

                  printf("\n");

             }

         

         }

};

int main()
{

    int n, m;

    cin>> n >> m;

    arraylist s;

    while(1) {

         

         char sttt[20];

         //  memset(sttt,'\0',sizeof(sttt[20]));

         if (scanf("%s", sttt) == EOF) {

             break;

         }

         student demo(n, m);

         string aa;

         aa= sttt;

         demo.name=aa;         

//       memset(demo,0,sizeof(demo));

         for (int i = 1; i <= n;i++) {

             demo.score[i]=    demo.time[i]=0;

             cin>>aa;

             string str = aa;

             int te = 0;

             for (int j = 0; j <aa.size(); j++) {

                  if (str[j]== '(') {

                      demo.score[i]= te;
                      te= 0;

                      continue;
                  }

                  if (str[j]== ')') {

                      demo.time[i]= te;
                      break;

                  }
                  if (str[j]== '-') {
                      te=0;
                      break;
                  }
                  if (str[j]>= '0'&&str[j]<='9') {
                      te= 10 * te + str[j] - '0';

                  } 
             }

             if (te == 0) {

                  demo.score[i]= 0;

                  demo.time[i]= 0;

             }
             if(demo.score[i]==0&& te!=0 && demo.time[i]==0) {

                  demo.score[i]=te;

                  demo.time[i]=0;
             }

         }

         s.push(demo);

    }

    s.output();

    return 0;

}

 

 

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!