解题思路:最简单的思路就是最繁琐的思路,对于直接用分数和条件判断,很容易得出孩子的排名
排名为:LWZWZLZLW
但是代码实现思路只能是依次进行判断来找出最优解,优化方法也只能是在判断过程中找出最快的判断条件
注意事项:优化方式,可以先判断相邻情况,即可最快判断出不符合条件的情况,然后利用总分相等即三家均为15分,且每家只有3个孩子进行判断,即可得出最终结果,因为最优解唯一,所以不必用正确条件来判断,不满足错误条件,即为正确结果
参考代码:
#include <stdio.h> #include <stdbool.h> #include <assert.h> #define MAX_GRADE 15 static bool is_right_sort(const int* player) { assert(player); int num_l = 1; int num_w = 1; int num_z = 0; int grade_l = 9; int grade_w = 8; int grade_z = 0; int i = 0; for (i = 2; i < 9; i++) { if (player[i] == player[i - 1])//如果两个相邻相等,不符合条件 return false; if (player[i] == 1) { num_l++; grade_l += 9 - i; } else if (player[i] == 2) { num_w++; grade_w += 9 - i; } else if (player[i] == 3) { num_z++; grade_z += 9 - i; } if (num_l > 3 || num_w > 3 || num_z > 3 || grade_l > 15 || grade_w > 15 || grade_z > 15) return false; } return true; } int main(void) { int player[9] = { 0 }; //L:1 //W:2 //Z:3 player[0] = 1; player[1] = 2; int i1 = 0; for (i1 = 3; i1 <= 3; i1++) { int i2 = 0; player[2] = i1; for (i2 = 1; i2 <= 3; i2++) { int i3 = 0; player[3] = i2; for (i3 = 1; i3 <= 3; i3++) { int i4 = 0; player[4] = i3; for (i4 = 1; i4 <= 3; i4++) { int i5 = 0; player[5] = i4; for (i5 = 1; i5 <= 3; i5++) { int i6 = 0; player[6] = i5; for (i6 = 1; i6 <= 3; i6++) { int i7 = 0; player[7] = i6; for (i7 = 1; i7 <= 3; i7++) { player[8] = i7; if (is_right_sort(player)) { //int i = 0; ////打印排名 //for (i = 0; i < 9; i++) //{ // if (player[i] == 1) // printf("第%d名是L家的\n", i + 1); // else if (player[i] == 2) // printf("第%d名是W家的\n", i + 1); // else if (player[i] == 3) // printf("第%d名是Z家的\n", i + 1); //} //打印最后一名 if (player[8] == 1) printf("L\n"); else if (player[8] == 2) printf("W\n"); else if (player[8] == 3) printf("Z\n"); return 0; } } } } } } } } return 0; }
0.0分
1 人评分
【绝对值排序】 (C语言代码)浏览:894 |
printf基础练习2 (C语言代码)浏览:796 |
C语言程序设计教程(第三版)课后习题3.7 (C语言代码)浏览:350 |
蚂蚁感冒 (C语言代码)浏览:1409 |
剪刀石头布 (C++代码)浏览:1814 |
C二级辅导-统计字符 (C语言代码)浏览:515 |
C语言程序设计教程(第三版)课后习题7.4 (C语言代码)浏览:476 |
多输入输出练习2 (C语言代码)浏览:1718 |
盐水的故事 (C语言代码)浏览:1616 |
10月月赛题解浏览:554 |