2019年第一届全国高校计算机能力挑战赛初赛编程题(C++)

举报
资源描述
2019年第一届全国高校计算机能力挑战赛初赛 个人解答源码及思路,非评分标准答案。(程序设计赛 C++) 1.电商促销某种商品时,希望通过打折鼓励消费者组团消费。已知团队消费金额=该团的人数和*商品单价。打折规则如下: 当组团消费者数量不满50人时,商品消费金额没有折扣;消费者数量大于等于50,但小于100人时,商品消费金额打9折;消费者数量达到或超过100人时,商品消费金额打85折。 现在有一批团购信息(含团购人数和商品单价,每人限购一件商品),请计算该团折扣后实际消费金额。 输入说明: 某团的人数和商品单价。 输出说明: 求该团的折扣后的实际消费金额。 输入样例: 100 50 输出样例: 4250 参考源码: 1. #include  2. using namespace std; 3. int main() { 4.     int number, unitPrice; 5.     double totalPrice = 0; 6.     cin >> number >> unitPrice; 7.     totalPrice = number * unitPrice; 8.     if (number >= 100) { 9.         totalPrice *= 0.85; 10.     } else if (number >= 50) { 11.         totalPrice *= 0.9; 12.     } 13.     cout << totalPrice; 14.     return 0; 15. } 算法思路: 模拟算法。 运行截图: 测试用例1(题目输入样例1,9折) 测试用例2(85折) 测试用例3(不打折) 2.对于给定的十进制正整数N(N<100000),将1到N(含N)之间的每个整数转成八进制,求转换后的所有八进制数中含7的总个数。 提示:某个数的八进制含7的个数可以参照下面的例子: 对于整数127,对应的八进制为177,其含7的个数为2。 输入说明: 输入整数N 输出说明: 输出含7的总个数 输入样例: 8 输出样例: 1 参考源码: 1. #include  2. using namespace std; 3. int convert(int n) { 4.     int sum = 0; 5.     int remainder; 6.     while (n) { 7.         remainder = n % 8; 8.         n = n / 8; 9.         if (remainder == 7) { 10.             sum++; 11.         } 12.     } 13.     return sum; 14. } 15. 16. int main() { 17.     int n; 18.     int total = 0; 19.     cin >> n; 20.     for (int i = 1; i <= n; i++) { 21.         total += convert(i); 22.     } 23.     cout << total << endl; 24.     return 0; 25. } 算法思路: 应用辗转相除法来做进制转换,不需要输出转换后的数,在转换过程中直接进行7数码的判断和累计。 运行截图: 测试用例1(题目输入样例1) 测试用例2(N为99999,取最大值) 测试用例3(N为1,取最小值) 3.输入N个整数,从中挑选符合以下规则的三种类型的数,分别输出。 (1)个位数字为1、4或7的数; (2)个位数字为2、5或8的数; (3)个位数字为3、6或9的数。 输入说明: 第一行输入整数个数N;第二行输入这N个整数。 输出说明: 分三行输出,第一行输出符合条件(1)的数据,第二行输出符合条件(2)的数据,第三行输出符合条件(3)的数据;备注:某行如果没有任何数据,输出空行。 输入样例: 10 11 12 13 24 25 26 37 38 39 40 输出样例: 11 24 37 12 25 38 13 26 39 参考源码: 1. #include  2. #include  3. using namespace std; 4. void computer(int data[], int n) { 5.     int remainder; 6.     for (int i = 0; i < n; i++) { 7.         remainder = data[i] % 10; 8.         if ((remainder == 1) || (remainder == 4) || (remainder == 7)) { 9.             cout << data[i] << " "; 10.         } 11.     } 12.     cout << endl; 13.     for (int i = 0; i < n; i++) { 14.         remainder = data[i] % 10; 15.         if ((remainder == 2) || (remainder == 5) || (remainder == 8)) { 16.             cout << data[i] << " "; 17.         } 18.     } 19.     cout << endl; 20.     for (int i = 0; i < n; i++) { 21.         remainder = data[i] % 10; 22.         if ((remainder == 3) || (remainder == 6) || (remainder == 9)) { 23.             cout << data[i] << " "; 24.         } 25.     } 26.     cout << endl; 27. } 28. 29. int main() { 30.     int n; 31.     cin >> n; 32.     int *data = (int *) malloc(n * sizeof(int)); 33.     for (int i = 0; i < n; i++) { 34.         cin >> data[i]; 35.     } 36.     computer(data, n); 37.     free(data); 38.     return 0; 39. } 算法思路: 模拟算法,求余数进行分类判断(1、4、7;2、5、8;3、6、9)。由于没有采用数组来进行三个分类的存放,所以在函数中进行了三次循环,依次输出三类数。 运行截图: 测试用例1(题目输入样例1) 测试用例2(输出第一行为空行) 测试用例3(输出第二行为空行) 测试用例4(输出第一行、第二行均为空行) 4.在N(N<30)名运动员参加的体操比赛中,有K(K<10)名裁判给每位运动员分别打分,按规则每名运动员最后得分需去掉一个最高分和一个最低分,然后把其他裁判的打分相加,计算得出该运动员的总得分(规定数据中每名运动员的总得分各不相同)。现在比赛完毕,根据总得分高低排序得出冠军、亚军。请你帮忙算出冠军和亚军,并输出他们的姓名。 输入说明: 第一行输入整数N和K。N表示运动员人数,K表示裁判人数;从第二行开始,依次分行输入这N名运动员的数据(数据格式为:姓名各裁判的整数打分,其中每名运动员的姓名不含空格,每名裁判的打分以空格分隔)。 输出说明: 输出冠军和亚军运动员的姓名,以单个空格分开他们的姓名。 输入样例: 5 4 Zhang 85 85 90 80 Wang 85 90 80 90 Zhao 90 92 85 90 Li 75 80 85 80 Yang 81 75 80 85 输出样例: Zhao Wang 参考源码: 1. #include  2. #include  3. 4. using namespace std; 5. struct player { 6.     string name; 7.     int totalScore; 8. }; 9. 10. bool cmp(player first, player second) { 11.     return first.totalScore > second.totalScore; 12. } 13. 14. int sum(int score[], int k) { 15.     int sum = 0; 16.     for (int i = 0; i < k; i++) { 17.         sum += score[i]; 18.     } 19.     if (k > 2) { 20.         sum -= score[0]; 21.         sum -= score[k - 1]; 22.     } 23.     return sum; 24. } 25. 26. int main() { 27.     int n, k; 28.     player players[30]; 29.     player onePlayer; 30.     int scores[10]; 31.     string name; 32.     int score; 33.     cin >> n >> k; 34.     for (int i = 0; i < n; i++) { 35.         cin >> name; 36.         onePlayer.name = name; 37.         for (int j = 0; j < k; j++) { 38.             cin >> score; 39.             scores[j] = score; 40.         } 41.         sort(scores, scores + k); 42.         onePlayer.totalScore = sum(scores, k); 43.         players[i] = onePlayer; 44.     } 45.     sort(players, players + n, cmp); 46.     if (n>1) { 47.         cout << players[0].name << " " << players[1].name << endl; 48.     }else{ 49.         cout << players[0].name<
展开阅读全文
温馨提示:
金锄头文库所有资源均是用户自行上传分享,仅供网友学习交流,未经上传用户书面授权,请勿作他用。
相关搜索

当前位置:首页 > IT计算机/网络 > 数据结构与算法


电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号