ACM 3 分步实现 打基础 再实现

对 ”ACM 2 分步实现 打基础 再实现“的改进。

需要对排名系统做一定的改进。
一种改进的方案是对选第i门课的每一个学生的成绩加上一个特定的修正值di,例如编号为j的学生该课的成绩Gij修改为G'ij=Gij+di。最终使得经过调整后,该课调整后的平均分等于未调整前选该课的所有学生所有课的平均分。你的任务是根据某一个班级学生某学期的成绩,计算每门课的修正值di。
输入学生人数m(1 <= m <= 20)、课程数目n(1 <= n <= 10)、课程名称以及各个学生各门课的成绩。对于第i门课程,输出修正值di。

输入:

1. 第一行输入两个整数,用空格分隔,分别为学生人数m和课程数目n。
2. 第二行输入n门课程名称,用空格分隔。每门课程的名称均不超过15个字符,均为小写。
3. 第三行开始输入m * n的矩阵。表示各个学生各门课成绩x(0 < x <= 100),以空格分隔。若学生未选此课,则该位输出0。

输出:

输出为n行,每行格式为“math 5”。首先输出课程名称,空格后输出修正值di。di为整数(计算过程中小数部分均舍去,不考虑)。Di可能为正值,也可能为负值。

输入样例:

8 5
math physics algebra english chemistry
98 78 0 76 86
0 79 99 89 68
0 0 79 96 78
58 97 79 90 47
90 0 84 99 77
94 54 76 85 0
69 60 0 85 95
79 85 86 96 68

输出样例:

math -1
physics 4
algebra -2
english -8
chemistry 7

  1 #include <stdio.h>
  2  #include <stdlib.h>
  3 
  4  // (y1 + y2 + .....y_count_in+(count_in)*detal) / count_in = n1 = sum / count_in
  5  // y1 表示本门课程的源成绩,
  6  //sum逐个加每个学生(已经选了该门课)所有课程的平均,到下面再除一个count_in就是 所有选这课的学生的 所有成绩 的平均值。
  7  // count_in表示选本门课的学生数
  8  // n1表示选了本门课的 所有学生 的 所有的成绩 的平均值。
  9  // detal即为所求的本门所需的差值。
 10  // => detal=(sum-(y1+y2....y_count_in)) / count_in
 11  // => detal= (sum  - totalOfClass ) / count_in.
 12  int f_pinjun(int data[][10],int student_num,int row,int class_num)
 13  {
 14      int count_in=0;
 15      int i,sum=0,j,n_stu_class_num,totalOfClass=0,class_count_stu;
 16      int each_pingjun[student_num];
 17      //deta=0;
 18 
 19     for(j=0;j<student_num;j++)
 20      {
 21          sum=0;
 22          class_count_stu=0;
 23          for(i=0;i<class_num;i++)
 24          {
 25              if(data[j][i]!=0)
 26              {
 27                  sum+=data[j][i];
 28                  class_count_stu++;
 29              }
 30          }
 31          each_pingjun[j]=sum/class_count_stu;
 32      }
 33 
 34     sum=0;
 35 
 36      for(i=0; i<student_num; i++)
 37      {
 38          if(data[i][row]!=0)
 39          {
 40              //tmp_sum=0;
 41              n_stu_class_num=0;
 42          /*    for(j=0;j<class_num;j++)//这个地方的重复的太多(对于第一个课程data[i][row]!=0 时 计算一次,10门复种就计算同一个学生的平均成绩10次,这完全没有必要)。
 43           * ,应当是预先保存每一个学生的平均值,然后调用,这样可以避免重复。
 44              {
 45                  if(data[i][j]!=0)
 46                  {
 47                      n_stu_class_num++;
 48                      tmp_sum+=data[i][j];
 49                  }
 50 
 51              }*/
 52 
 53              totalOfClass+=data[i][row];
 54              //tmp_sum/=n_stu_class_num;
 55              count_in++;
 56              //sum+=tmp_sum;//sum逐个加每个学生(已经选了该门课)所有课程的平均,到下面再除一个count_in就是 所有选这课的学生的 所有成绩 的平均值。
 57              sum+=each_pingjun[i];
 58          }
 59      }
 60      //deta=totalOfClass/count_in;
 61      //return sum/count_in;
 62      return (sum-totalOfClass)/count_in;
 63  }
 64 
 65  int main()
 66  {
 67      char class_name[10][15];
 68      int student_num,class_num,i,data[20][10],j;
 69      //int pinjun[10]= {0,0};
 70      //int each_pingjun[20];
 71      scanf("%d %d",&student_num,&class_num);
 72      //printf("%d %d\n",student_num,class_num);
 73 
 74      for(i=0; i<class_num; i++)
 75      {
 76          scanf("%s",class_name[i]);
 77      }
 78 
 79      for(i=0; i<student_num; i++)
 80      {
 81          for(j=0; j<class_num; j++)
 82              scanf("%d",&data[i][j]);
 83      }
 84 
 85      //fpin_jun_each(data,each_pingjun,student_num,class_num);//
 86      //print_data(data,student_num,class_num);
 87 
 88      for(i=0; i<class_num; i++)
 89      {
 90          //pinjun[i]=f_pinjun(data,student_num,i,class_num);
 91          //printf("%d ",pinjun[i]);
 92          //if(i!=class_num-1)
 93          //printf("%s %d\n",class_name[i],pinjun[i]-deta);
 94          //printf("%d\n",deta);
 95          printf("%s %d\n",class_name[i],f_pinjun(data,student_num,i,class_num));
 96      }
 97      //printf("%s %d",class_name[i-1],pinjun[i]-deta);
 98 
 99      return 0;

100  } 

 

posted @ 2012-05-14 17:14  zhengmian  阅读(400)  评论(0)    收藏  举报