OJ第六批——Problem N: 填空题:静态成员---计算学生个数

2024-04-10 02:08

本文主要是介绍OJ第六批——Problem N: 填空题:静态成员---计算学生个数,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

问题及代码:

 

Problem N: 填空题:静态成员---计算学生个数

Time Limit: 1 Sec   Memory Limit: 128 MB
Submit: 234   Solved: 173
[ Submit][ Status][ Web Board]

Description

学生类声明已经给出,在主程序中根据输入信息输出实际建立的学生对象个数,以及所有学生对象的成绩总和。
在下面的程序段基础上完成设计,只提交begin到end部分的代码
#include <iostream>
#include <string>
using namespace std;
class student
private:
string name;  //学生姓名
int age;      //学生年龄
int score;    //学生成绩
static int count; //记录学生对象个数
static int sum;  //记录所有学生的总成绩
public:
student(string n,int a,int s);  //构造函数
static int get_count();  //静态成员函数,获取count的值
static int get_sum();   //静态成员函数,获取sum的值
};
 
//将程序需要的成份写下来,只提交begin到end部分的代码
//******************** begin ********************
int student::count=0;
_____(1)_______;
________(2)___________
{
name=n;
age=a;
score=s;
count++;
sum+=s;
}
int student::get_count()
{
______(3)_______;
}
int student::get_sum()
{
______(4)______;
}
//********************* end ********************
int  main( )
{
string name;
int age;
int score;
int n;
cin>>n;  //输入学生对象个数
while(n--)
{
cin>>name>>age>>score;
new student(name,age,score);
}
cout<<"the count of student objects=";
cout<<student::get_count()<<endl;
cout<<"the sum of all students score=";
cout<<student::get_sum()<<endl;
return 0;
}

Input

学生个数

对应学生个数的学生信息(姓名    年龄    成绩)

Output

学生个数

所有学生的成绩之和

Sample Input

3
guo  34  98
zhang    56  60
li   23   87

Sample Output

the count of student objects=3
the sum of all students score=245

HINT

#include <iostream> 
#include <string> 
using namespace std; 
class student 
{  
private: 
string name;  //学生姓名 
int age;      //学生年龄 
int score;    //学生成绩 
static int count; //记录学生对象个数 
static int sum;  //记录所有学生的总成绩 
public: 
student(string n,int a,int s);  //构造函数 
static int get_count();  //静态成员函数,获取count的值 
static int get_sum();   //静态成员函数,获取sum的值 
}; 
int student::count=0; 
int student::sum=0; 
student::student(string n,int a,int s) 
{ 
name=n; 
age=a; 
score=s; 
count++; 
sum+=s; 
} 
int student::get_count() 
{ 
return count; 
} 
int student::get_sum() 
{ 
return sum; 
} 
int main() 
{ 
string name; 
int age; 
int score; 
int n; 
cin>>n;  //输入学生对象个数 
while(n--) 
{ 
cin>>name>>age>>score; 
new student(name,age,score); 
} 
cout<<"the count of student objects="; 
cout<<student::get_count()<<endl; 
cout<<"the sum of all students score="; 
cout<<student::get_sum()<<endl; 
return 0; 
} 


 

 

这篇关于OJ第六批——Problem N: 填空题:静态成员---计算学生个数的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/889808

相关文章

Python如何计算两个不同类型列表的相似度

《Python如何计算两个不同类型列表的相似度》在编程中,经常需要比较两个列表的相似度,尤其是当这两个列表包含不同类型的元素时,下面小编就来讲讲如何使用Python计算两个不同类型列表的相似度吧... 目录摘要引言数字类型相似度欧几里得距离曼哈顿距离字符串类型相似度Levenshtein距离Jaccard相

解读静态资源访问static-locations和static-path-pattern

《解读静态资源访问static-locations和static-path-pattern》本文主要介绍了SpringBoot中静态资源的配置和访问方式,包括静态资源的默认前缀、默认地址、目录结构、访... 目录静态资源访问static-locations和static-path-pattern静态资源配置

使用C#代码计算数学表达式实例

《使用C#代码计算数学表达式实例》这段文字主要讲述了如何使用C#语言来计算数学表达式,该程序通过使用Dictionary保存变量,定义了运算符优先级,并实现了EvaluateExpression方法来... 目录C#代码计算数学表达式该方法很长,因此我将分段描述下面的代码片段显示了下一步以下代码显示该方法如

如何用Java结合经纬度位置计算目标点的日出日落时间详解

《如何用Java结合经纬度位置计算目标点的日出日落时间详解》这篇文章主详细讲解了如何基于目标点的经纬度计算日出日落时间,提供了在线API和Java库两种计算方法,并通过实际案例展示了其应用,需要的朋友... 目录前言一、应用示例1、天安门升旗时间2、湖南省日出日落信息二、Java日出日落计算1、在线API2

csu 1446 Problem J Modified LCS (扩展欧几里得算法的简单应用)

这是一道扩展欧几里得算法的简单应用题,这题是在湖南多校训练赛中队友ac的一道题,在比赛之后请教了队友,然后自己把它a掉 这也是自己独自做扩展欧几里得算法的题目 题意:把题意转变下就变成了:求d1*x - d2*y = f2 - f1的解,很明显用exgcd来解 下面介绍一下exgcd的一些知识点:求ax + by = c的解 一、首先求ax + by = gcd(a,b)的解 这个

spoj705( 求不相同的子串个数)

题意:求串s的不同子串的个数 解题思路:任何子串都是某个后缀的前缀,对n个后缀排序,求某个后缀的前缀的个数,减去height[i](第i个后缀与第i-1 个后缀有相同的height[i]个前缀)。 代码如下: #include<iostream>#include<algorithm>#include<stdio.h>#include<math.h>#include<cstrin

uva 10025 The ? 1 ? 2 ? ... ? n = k problem(数学)

题意是    ?  1  ?  2  ?  ...  ?  n = k 式子中给k,? 处可以填 + 也可以填 - ,问最小满足条件的n。 e.g k = 12  - 1 + 2 + 3 + 4 + 5 + 6 - 7 = 12 with n = 7。 先给证明,令 S(n) = 1 + 2 + 3 + 4 + 5 + .... + n 暴搜n,搜出当 S(n) >=

poj 1113 凸包+简单几何计算

题意: 给N个平面上的点,现在要在离点外L米处建城墙,使得城墙把所有点都包含进去且城墙的长度最短。 解析: 韬哥出的某次训练赛上A出的第一道计算几何,算是大水题吧。 用convexhull算法把凸包求出来,然后加加减减就A了。 计算见下图: 好久没玩画图了啊好开心。 代码: #include <iostream>#include <cstdio>#inclu

uva 1342 欧拉定理(计算几何模板)

题意: 给几个点,把这几个点用直线连起来,求这些直线把平面分成了几个。 解析: 欧拉定理: 顶点数 + 面数 - 边数= 2。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <cmath>#inc

uva 11178 计算集合模板题

题意: 求三角形行三个角三等分点射线交出的内三角形坐标。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <cmath>#include <stack>#include <vector>#include <