一道简单的C语言试题,该如何处理
一道简单的C语言试题
对于一次考试成绩进行统计,考M科,(如六科,语文,政治,数学,物理,化学,外语),有N人(如10000人)参加。
以个人总分的成绩排序,最高分排在第一名,最后输出的表格如下所示。
名次 考号 姓名 语文 政治 数学 物理 化学 外语 总分
------------------------------
1
2
3
------------------------------
我写的程序如下,但是有问题,请大家帮我看看
#include <stdio.h>
int main()
{
int i=0;
int j=0;
int t1;
int sum1=0;
int sum2=0;
int num=0;
int sum=0;
/*
printf( "Please input students number: ");
scanf( "%d ",&num);
*/
int array[5][8];
while(i <5)
{
printf( "Please input No.%d student 's score\n ",i+1);
array[i][0]=i+1;
printf( "Chinese 's score: ");
scanf( "%d ",&array[i][1]);
printf( "Politic 's score: ");
scanf( "%d ",&array[i][2]);
printf( "Math 's score: ");
scanf( "%d ",&array[i][3]);
printf( "Physic 's score: ");
scanf( "%d ",&array[i][4]);
printf( "Chemistry 's score: ");
scanf( "%d ",&array[i][5]);
printf( "English 's score: ");
scanf( "%d ",&array[i][6]);
printf( "--------------------------------------\n ");
array[i][7]=array[i][1]+array[i][2]+array[i][3]+array[i][4]+array[i][5]+array[i][6];
i++;
}
printf( "\nId\tNo\tChi\tPoli\tMath\tPhy\tChem\tEng\tSum\n ");
printf( "------------------------------------\n ");
for(t1=0;t1 <5;t1++)
{
if(t1> =4) continue;
if(array[t1][7] <array[t1+1][7])
{
int t2;
int temp=0;
for(t2=0;t2 <8;t2++)
{
temp=array[t1][t2];
array[t1][t2]=array[t1+1][t2];
array[t1+1][t2]=temp;
}
}
}
while(j <5)
{
printf( "%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n ",j+1,array[j][0],array[j][1],array[j][2],array[j][3],array[j][4],array[j][5],array[j][6],array[j][7]);
j++;
}
printf( "------------------------------------\n ");
return 0;
}
------解决方案--------------------
if(array[t1][7] <array[t1+1][7])
{
int t2;
int temp=0;
for(t2=0;t2 <8;t2++)
{
temp=array[t1][t2];
array[t1][t2]=array[t1+1][t2];
array[t1+1][t2]=temp;
}
}
这个排序算法有问题
你只能保证最后一名排序正确,前几名的排序都是错的
应该用两次循环
int n1, n2, m, temp;
for (n1 = 0; n1 < m - 1; n1++)
{
for (n2 = n1 + 1; n2 < m; n2++)
{
if (a[n1] < a[n2])
{
temp = a[n1];
a[n1] = a[n2];
a[n2] = temp;
// 可以用你的算法替换这个swap算法
}
}
}
第一次循环把分最高的放在第一位,依次类推
对于一次考试成绩进行统计,考M科,(如六科,语文,政治,数学,物理,化学,外语),有N人(如10000人)参加。
以个人总分的成绩排序,最高分排在第一名,最后输出的表格如下所示。
名次 考号 姓名 语文 政治 数学 物理 化学 外语 总分
------------------------------
1
2
3
------------------------------
我写的程序如下,但是有问题,请大家帮我看看
#include <stdio.h>
int main()
{
int i=0;
int j=0;
int t1;
int sum1=0;
int sum2=0;
int num=0;
int sum=0;
/*
printf( "Please input students number: ");
scanf( "%d ",&num);
*/
int array[5][8];
while(i <5)
{
printf( "Please input No.%d student 's score\n ",i+1);
array[i][0]=i+1;
printf( "Chinese 's score: ");
scanf( "%d ",&array[i][1]);
printf( "Politic 's score: ");
scanf( "%d ",&array[i][2]);
printf( "Math 's score: ");
scanf( "%d ",&array[i][3]);
printf( "Physic 's score: ");
scanf( "%d ",&array[i][4]);
printf( "Chemistry 's score: ");
scanf( "%d ",&array[i][5]);
printf( "English 's score: ");
scanf( "%d ",&array[i][6]);
printf( "--------------------------------------\n ");
array[i][7]=array[i][1]+array[i][2]+array[i][3]+array[i][4]+array[i][5]+array[i][6];
i++;
}
printf( "\nId\tNo\tChi\tPoli\tMath\tPhy\tChem\tEng\tSum\n ");
printf( "------------------------------------\n ");
for(t1=0;t1 <5;t1++)
{
if(t1> =4) continue;
if(array[t1][7] <array[t1+1][7])
{
int t2;
int temp=0;
for(t2=0;t2 <8;t2++)
{
temp=array[t1][t2];
array[t1][t2]=array[t1+1][t2];
array[t1+1][t2]=temp;
}
}
}
while(j <5)
{
printf( "%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n ",j+1,array[j][0],array[j][1],array[j][2],array[j][3],array[j][4],array[j][5],array[j][6],array[j][7]);
j++;
}
printf( "------------------------------------\n ");
return 0;
}
------解决方案--------------------
if(array[t1][7] <array[t1+1][7])
{
int t2;
int temp=0;
for(t2=0;t2 <8;t2++)
{
temp=array[t1][t2];
array[t1][t2]=array[t1+1][t2];
array[t1+1][t2]=temp;
}
}
这个排序算法有问题
你只能保证最后一名排序正确,前几名的排序都是错的
应该用两次循环
int n1, n2, m, temp;
for (n1 = 0; n1 < m - 1; n1++)
{
for (n2 = n1 + 1; n2 < m; n2++)
{
if (a[n1] < a[n2])
{
temp = a[n1];
a[n1] = a[n2];
a[n2] = temp;
// 可以用你的算法替换这个swap算法
}
}
}
第一次循环把分最高的放在第一位,依次类推