1、输入学生信息,姓名成绩(成绩的数目不一定)输出每个学生的学号和平均成绩,以及不及格课程数超过2的学生,按不及格课程数从大到小排好序输出。
input:
stu1
60 70 80 30
stu2
10 20 30 40 50
stu3
10 20 30 40 50 60 30
stu4
60 80 100
stu5
50 40 30 60 70
#
output:
stu1 60
stu2 30
stu3 34.2857
stu4 80
stu5 50
不及格课程数超过2的学生有:
stu3 34.2857
stu2 30
stu5 50
#include <iostream> #include <vector> #include <algorithm> using namespace std; struct student { string name; vector<int> grade; int lownum = 0; double avgs; }; bool cmp(student s1, student s2) { return s1.lownum > s2.lownum; } int main() { vector<student> v, vv; string s; cout << "请输入学生信息:姓名,成绩…… 输入#结束" << endl; while(cin >> s) { if(s == "#") break; student stu; stu.name = s; int score, sum = 0, cnt = 0; while(cin >> score) { stu.grade.push_back(score); if(score < 60) stu.lownum++; sum += score; cnt++; if(getchar() == ‘\n‘) break; } stu.avgs = sum / (double)cnt; v.push_back(stu); if(stu.lownum > 2) vv.push_back(stu); } sort(vv.begin(), vv.end(), cmp); for(int i = 0; i < v.size(); i++) { cout << v[i].name << " " << v[i].avgs << endl; } cout << "不及格课程数超过2的学生有:" << endl; for(int i = 0; i < vv.size(); i++) { cout << vv[i].name << " " << vv[i].avgs << endl; } return 0; }
2、输入字符串,输出字符串中包含的数字
input: 2.3ABC0-2.3
output:2.3 0 -2.3。
input: +004.500
output:4.5
(原本输出+4.5,但我感觉应该不需要‘+‘,具体问题具体对待吧)
#include <iostream> #include <vector> using namespace std; int main() { string s; while(cin >> s) { string str; vector<string> v; int dot; for(int i = 0; i < s.length(); i++) { if (s[i] == ‘+‘) continue; if (!isalpha(s[i])) str += s[i]; if (isalpha(s[i]) || i == s.length() - 1 || s[i + 1] == ‘-‘) { if (str != "") { if(str.length() != 1) { int num = 0, num1, flag = 0, num2; for(int j = 0; j < str.length(); j++) { if(str[j] == ‘0‘) num++; if(str[j] != ‘0‘ && !flag) { flag++; num1 = num; } } num = 0;flag = 0; for(int j = str.length() - 1; j >= 0; j--) { if(str[j] == ‘0‘) num++; if(str[j] != ‘0‘ && !flag) { flag++; num2 = num; } } str = str.substr(num1, str.length() - num2 - num1); } v.push_back(str); str = ""; } } } for(int i = 0; i < v.size(); i++) { cout << v[i]; if(i < v.size() - 1) cout << " "; } cout << endl; } return 0; }
PS: 第二题好像还不完善,抽空再改吧!
后面的有保研夏令营题目,也都是网上的,有时间更
原文地址:https://www.cnblogs.com/ache/p/12585215.html
时间: 2024-10-20 18:45:56