//同分怎么办？怎么排名？
#include
#include
#include
#include
#include
using namespace std;
void main()
{
int q;
int d[30];
int k;
int vec1[30];//总分1，之后要排序的
int n,sum=0;
string course[] = { "MT","EN","PH" };//科目
int a[30];//学生编号
int c[30];//总分2，原件
double b;
cin >> n;
int v = n;
for (int i = 1; i <= n; i++)
a[i] = i;//学生编号

``````const int m = sizeof(a[30]);
double score[m][3];
for (int i = 1; i <= n; i++)
{
for (int j = 0; j < 3; j++)
{
cout << a[i] << "号的" << course[j] << "成绩：" << endl;
cin>>score[i][j];
}
}
for (int i = 1; i <= n; i++)
{
for (int j = 0; j < 3; j++)
{
sum += score[i][j];
}
vec1[i] = sum;
c[i] = sum;
cout <<a[i]<< "号总分：" << sum << endl <<a[i]<< "号平均分：" << sum / 3 << endl;
sum = 0;

}
int temp = 0;
for (int i = n; i >=1; i--)
{
for (int j = i - 1; j >=1; j--)
if (vec1[i] > vec1[j])
{
temp = vec1[j];
vec1[j] = vec1[i];
vec1[i] = temp;

}
}

cout << endl;
for (int i = 1; i <= v; i++)
{
for (int j = 1; j <= n; j++)
{
if (c[j] == vec1[i])
{
cout << "第" << i << "名是：" << j << "号" << endl;
d[j] = i;//第几号是第几名
}
}

}
cout << "学生编号" << '\t' << course[0] << '\t' << course[1] << '\t' << course[2] << '\t' << "总分" << '\t' << "平均分" << '\t' << "排名" << endl;
for (int i = 1; i <= n; i++)
{
cout <<setw(8)<< i << '\t';
for (int j = 0; j < 3; j++)
{
cout << score[i][j];
cout << '\t';
}
cout << setw(4) << c[i] << '\t';
cout << setw(6) << c[i] / 3 << '\t';
cout << setw(4) << d[i] << endl;

}
cout << "请输入要查找的学号:";
bool o = false;
cin >> q;
for (int i = 1; i <= n; i++)
{
if (q == a[i])
{
cout << "学生编号" << '\t' << course[0] << '\t' << course[1] << '\t' << course[2] << '\t' << "总分" << '\t' << "平均分" << '\t' << "排名" << endl;

cout << setw(8) << i << '\t';
for (int j = 0; j < 3; j++)
{
cout << score[i][j];
cout << '\t';
}
cout << setw(4) << c[i] << '\t';
cout << setw(6) << c[i] / 3 << '\t';
cout << setw(4) << d[i] << endl;
o = true;
}

}
if (o == false)
cout << "该学号不存在" << endl;
``````

}

1个回答

``````for (int i = n; i >=1; i--)
{
for (int j = i - 1; j >=1; j--)
if (vec1[i] > vec1[j]) || (vec1[i] == vec1[j] && 第二条件前者大于后者)
{

temp = vec1[j];
vec1[j] = vec1[i];
vec1[i] = temp;
}
}
``````
DarkmoonYZW ”&& 第二条件前者大于后者“能详细说明一下吗，谢谢
3 个月之前 回复