1012. The Best Rank (25)

来源:互联网 发布:半人马数据钢铁力量 编辑:程序博客网 时间:2024/05/10 09:05

注意超时问题,先排序好再进行查找就不会超时,查找时最好二分法查找,不过顺序查找也没有超时问题就不再修改了。

#define  _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <math.h>
#include <string>
#include <string.h>
#include <vector>  
using namespace std;
struct Student
{
int ID, C, M, E, A;
int grade[4];//C, M, E, A
int rank[4];
Student()
{
ID = 0; 
grade[0] = 0; 
grade[1] = 0; 
grade[2] = 0; 
grade[3] = 0;
rank[0] = 1; 
rank[2] = 1;
rank[3] = 1;
rank[4] = 1;
}
};
void FindMost(int *rank,char &c,int &best)
{
best = rank[3];
int  mark = 3;
for (int i = 0; i < 3; i++)
{
if (rank[i] < best)
{
best = rank[i];
mark = i;
}
}
if (mark == 3)
{
c = 'A';
}
else if (mark == 0)
{
c = 'C';
}
else if (mark == 1)
{
c = 'M';
}
else if (mark == 2)
{
c = 'E';
}
}
int main()
{
//StudentID  C  M  E  A
//310101     98 85 88 90
int N, M;
cin >> N >> M;
Student *stu = new Student[N];
for (int i = 0; i < N; i++)

cin >> stu[i].ID >> stu[i].grade[0] >> stu[i].grade[1] >> stu[i].grade[2];
stu[i].grade[3] = (int)(((float)(stu[i].grade[0] + stu[i].grade[1] + stu[i].grade[2])) / 3.0 + 0.5);

int i_gr = 3;
while (i_gr >= 0)
{
int rank = 1;
for (int j = 0; j < i; j++)
{
if (stu[j].grade[i_gr] > stu[i].grade[i_gr])
{
rank++;
}
else if (stu[j].grade[i_gr] == stu[i].grade[i_gr])
{
stu[i].rank[i_gr] = stu[j].rank[i_gr];
//rank++;
}
else
{
stu[j].rank[i_gr] += 1;
}
}
stu[i].rank[i_gr] = rank;
i_gr--;
}

}

for (int i = 0; i < M; i++)
{
int i_ID;
cin >> i_ID;
bool bFind = false;
for (int j = 0; j < N; j++)
{
if (i_ID == stu[j].ID)
{
bFind = true;
char c = ' ';
int rank = 1;
FindMost(stu[j].rank, c, rank);
cout << rank << " " << c << endl;
break;
}
}

if (!bFind)
{
cout << "N/A\n";
}
}
return 0;
}
0 0
原创粉丝点击