当前位置:网站首页>zstuAcm学生信息库的建立(用链表完成)
zstuAcm学生信息库的建立(用链表完成)
2022-07-22 18:10:00 【潇湘夜雨寒】
初步掌握了链表的建立,下面我们再来一个简单的链表的操作。
输入一些学生的信息,包括学号、姓名、成绩。
现在要求你用函数创建一个学生库的链表。
struct node
{
…
};
struct node * Create_Stu_Doc();
完成函数内链表的创建工作。
输入
输入一些学生的信息,每个学生信息一行,分别为学号、姓名和成绩,中间用空格隔开,其中学号和成绩均为整数,姓名为不超过15个仅包含大小写字母的字符。
如果输入的一行是非正整数时,表示结束
输出
按照输入的顺序建立链表,并输出所有高于平均分同学的学号和分数,每行2个整数,中间用空格隔开。
样例输入 Copy
1001 xiangwang 90
1002 xiaoli 85
1003 xiaohong 97
1004 xiaoma 76
-1
样例输出 Copy
1001 90
1003 97
实际上原题下面有模板代码,但是个人认为STL是个非常方便实用的工具,所以想试试用STL去完成这道题
开头和其他的模板没什么,直到后来要用到find_if(begin(),end(),function)函数。
前两个参数不难,难的是最后那个,要用到类与对象,尽管acm比赛中,用到类与对象的次数屈指可数,可能甚至用不到,但是,作为c++的使用者,未来到工作里类与对象是重中之重,所以决定在此写一次。
#include <iostream>
#include<cstdio>
#include<string>
#include<list>
#include<vector>
using namespace std;
//定义结构最好这样,规范!!!!
typedef struct tag_students
{
int no;
string name;
int grade;
}students;
class CFinderGeaterNum
{
public:
CFinderGeaterNum(int nNum)
{
m_nNum = nNum;
}
bool operator()(const std::list<students>::value_type &value)
{
if (value.grade > m_nNum)
{
return true;
}
return false;
}
private:
int m_nNum;
};
int main()
{
list<students>stu1;
students stu2;
int ave = 0;
while (1)
{
int no1 = 0;//输入的学生学号
string name1;//输入的姓名
int grade1 = 0;//输入的成绩
cin >> no1;
if (no1 <= 0)//如果输入的学号<=0,那么推出循环
{
break;
}
cin >> name1 >> grade1;
stu2.no = no1;
stu2.grade = grade1;
stu2.name = name1;
ave = ave + stu2.grade;//求平均值,后面会除总数
stu1.push_back(stu2);
}
ave = ave / stu1.size() * 1.0;
int xxxxxx = 5;
list<students>::iterator val_begin = stu1.begin();
while (true)
{
list<students>::iterator it = find_if(val_begin, stu1.end(),
CFinderGeaterNum(ave));
if (it != stu1.end())
{
//找个一个
cout << it->no <<" " << it->grade << endl;
//继续找
it++;
val_begin = it;
continue;
}
else
{
//没有了
break;
}
}
return 0;
}
//!注意一个大问题,不知为啥acm里使用stl容易时间超限(一般来说stl速度也不慢),所以这时,要解决方法也简单,把cin与cout给换成scanf和printf,这两个的速度要比cin与cout快很多倍!
边栏推荐
猜你喜欢

Day04 --- use of Zen

About count=count++

Learn amber tutorial A17: umbrella sampling and drawing the potential energy surface of alanine tripeptide

2021-06-03pip报错 ValueError: Unable to find resource t64.exe in package pip._vendor.distlib

Crawl web pages to dynamically load comments

After class exercise 02 --- phone number of a city

读刘润《底层逻辑》摘录

禅道的安装和使用&缺陷报告&缺陷作业

Selenium基础知识 自动登录百度网盘

Amber tutorial 4.6: hydrogen bond analysis of the system
随机推荐
Software testing ~ learning / advanced route
Selenium基础知识 调试方法
Shell编程规范与变量
selenium使用
Selenium基础知识 多窗口处理
关系型数据库10 分钟了解 MySQL
Learn amber tutorial A17: umbrella sampling and drawing the potential energy surface of alanine tripeptide
十月随笔
软件测试基础
数字验证码识别完成自动化登录
存储过程
关于count=count++
测试用例:注册邮箱
Selenium基础知识 自动登录百度网盘
读《机器学习-周志华》内容摘要
Appium automated testing, instance sharing
Basic process of sales service
判断 Map 中是否包含指定的 key 和 value
Software testing is divided by stages: unit testing, integration testing, system testing, acceptance testing
Shallow shaking automatic test