华为软件编程题:选秀节目打分
1 /* cal_score.cpp 2 * 选秀节目打分,分为专家评委和大众评委, 3 * score[] 数组里面存储每个评委打的分数, 4 * judge_type[] 里存储与 score[] 数组对应的评委类别, 5 * judge_type[i] == 1,表示专家评委, 6 * judge_type[i] == 2,表示大众评委,n表示评委总数。 7 * 打分规则如下:专家评委和大众评委的分数先分别取一个平均分(平均分取整), 8 * 然后,总分 = 专家评委平均分 * 0.6 + 大众评委 * 0.4,总分取整。 9 * 如果没有大众评委,则 总分 = 专家评委平均分,总分取整。函数最终返回选手得分。 10 */ 11 12 #include <stdio.h> 13 14 #define N 5 15 16 int cal_score(int score[], int judge_type[], int n) 17 { 18 int expert = 0; //专家评委的总分 19 int general = 0; //大众评委的总分 20 int total = 0; 21 int i = 0; 22 int number = 0; 23 24 for (i = 0; i < n; i++) 25 { 26 if (judge_type[i] == 1) 27 { 28 expert += score[i]; 29 number++; 30 } 31 else 32 { 33 general += score[i]; 34 } 35 } 36 37 if (number == n) 38 { 39 total = (int)(expert / n); 40 } 41 else 42 { 43 expert = (int)(expert / n); 44 general = (int)(general / n) ; 45 total = (int)(0.6 * expert + 0.4 * general); 46 } 47 48 return total; 49 } 50 51 int main() 52 { 53 int score[N]; 54 int judge_type[N]; 55 int i = 0; 56 int total = 0; 57 58 for (i = 0; i < N; i++) 59 { 60 printf("Please input the score %d and the judge type:", i + 1); 61 scanf("%d%d", &score[i], &judge_type[i]); 62 if ((score[i] < 0) || (score[i] > 100) || (judge_type[i] < 1) || (judge_type[i] > 2)) 63 { 64 printf("Input error!\n"); 65 return 0; 66 } 67 } 68 69 total = cal_score(score, judge_type, N); 70 printf("The total score is %d!\n", total); 71 return 0; 72 } 73 74 75 //以上为自己写的 76 //下面为网上的答案 77 78 //#include <stdio.h> 79 //#include <string.h> 80 //#include <iostream> 81 //#include <conio.h> 82 //#define N 5 83 // 84 //int cal_score(int score[], int judge_type[], int n) 85 //{ 86 // int expert = 0; 87 // int dazhong = 0; 88 // int zongfen = 0; 89 // int i; 90 // int number = 0; 91 // 92 // for (i = 0; i < N; i++) 93 // { 94 // if (judge_type[i] == 1) 95 // { 96 // expert = expert + score[i]; 97 // number++; 98 // } 99 // else dazhong = dazhong + score[i]; 100 // } 101 // if (number == N) 102 // { 103 // zongfen = (int)(expert / N); 104 // } 105 // else 106 // { 107 // expert = (int)(expert / N); 108 // dazhong = (int)(dazhong / N); 109 // zongfen = (int)(0.6 * expert + 0.4 * dazhong); 110 // } 111 // return zongfen; 112 //} 113 // 114 //int main() 115 //{ 116 // int score[N]; 117 // int judge_type[N]; 118 // int numberlast = 0; 119 // int i; 120 // printf("please input the %d score:\n", N); 121 // for (i = 0; i < N; i++) 122 // { 123 // scanf("%d", &score[i]); 124 // } 125 // 126 // printf("please input the level(1:expert, 2:dazhong)\n"); 127 // for (i = 0; i < N; i++) 128 // { 129 // scanf("%d", &judge_type[i]); 130 // } 131 // numberlast = cal_score(score, judge_type, N); 132 // printf("the last score is %d\n", numberlast); 133 // return 0; 134 //}