坑点:不要把6为的学号当int型储存,要按照字符串储存,否则如果是001101,输入1101,则按照int型可以匹配,测试点第一个就是测试这个。
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
int N, M;
cin >> N >> M;
int table[2000][4];//table[i][0],到table[i][3]依次存入总分,C,M,E的成绩
char id[2000][7];//学号
int rank[2000][4] = { 0 };//每位同学各科排名和总排名
int best[2000][2] = { 0 };//最佳排名,和对应的科目
for (int i = 0; i < N; i++)
{
int sum = 0;
cin >> id[i];
id[i][6] = '\0';
for (int k = 0; k < 4; k++)
{
if (k == 0)
continue;
cin >> table[i][k];
sum += table[i][k];
}
table[i][0] = sum;//计算总分存入table[i][0]
}
for (int i = 0; i < N; i++)
{
int bestrank = -1;
int bestcourse;
for (int j = 0; j < 4; j++)//j对应科目
{
int max = 0;
for (int k = 0; k < N; k++)
{
if (table[i][j] < table[k][j])
{
if (max < table[k][j])
max = table[k][j];
rank[i][j]++;
}
}
if (bestrank > rank[i][j] || bestrank == -1)//每位学生最优秀科目
{
bestrank = rank[i][j];
bestcourse = j;
}
}
best[i][0] = bestrank+1;//rank数组初始化为0,因此每个人的名次要加1
best[i][1] = bestcourse;
}
for (int i = 0; i < M; i++)
{
int k;
char nid[7];
cin >> nid;
nid[6] = '\0';
for (k = 0; k < N+1; k++)
{
if(strcmp(nid,id[k])==0)
break;
}
if (k != N + 1)
{
cout << best[k][0] << ' ';
switch (best[k][1])
{
case 0:cout << 'A'; break;
case 1:cout << 'C'; break;
case 2:cout << 'M'; break;
case 3:cout << 'E'; break;
}
}
else
cout << "N/A";
cout << endl;
}
system("pause");
return 0;
}