华南师大17级计算机学院专硕上机题
一、输入一个数字,为其高,一个符号*,输出该符号组成的平行四边形形状。#include <iostream>using namespace std;int main() {int num;cin >> num;//输入数字for (int i = 1; i < num+1; i++) {//打印num行for (int j = 1; j < i; j++) {/
·
一、输入一个数字,为其高,一个符号*,输出该符号组成的平行四边形形状。
#include <iostream>
using namespace std;
int main() {
int num;
cin >> num; //输入数字
for (int i = 1; i < num+1; i++) { //打印num行
for (int j = 1; j < i; j++) { //打印空格
cout << " ";
}
for (int j = 1; j < num + 1; j++) { //打印*
cout << "*";
}
cout << endl; //换行
}
}
二、是猴子吃桃问题,每天都吃剩下的一半,再多吃一个,直到第十天剩下一个桃子,问第一天猴子有多少个桃。
#include <iostream>
using namespace std;
int cal(int n) {
if (n == 10) {
return 1; //第十天只剩一个桃子
}
else {
return (cal(n + 1) + 1 )* 2; //根据题目意思列出等式
}
}
int main() {
int answer = cal(1); //调用递归算法,传入参数1(第一天开始计算)
cout << "总共有:" << answer << "个桃子";
return 0;
}
三、输入一系列的数字,输出他们正负数个数,输入0截止,要用函数实现 。
#include <iostream>
using namespace std;
int count1 = 0, count2 = 0;
void judge(int temp) {
if (temp > 0) {
count1++;
}
else {
count2++;
}
}
int main() {
double temp;
cin >> temp;
while (abs(temp)>1e-6) { //判断是否等于0
judge(temp); //调用函数
cin >> temp;
}
cout << "正数:" << count1 << "个" << endl; //输出
cout << "负数:" << count2 << "个" << endl;
return 0;
}
四、用一个类来记录学生成绩和他班级,里面有几个函数,静态成员和动态成员都有。
#include <iostream>
#include<string>
using namespace std;
class Student {
private:
string name; //学生姓名
double score; //学生分数
string className; //学生班级名
static double sum; //全部总分
static int stuNum; //学生总数
public:
Student(string n, double s, string c); //有参构造函数
static double getSum(); //求总分
static double getAverage(); //求平均分
void stuDisplay(); //输出每个学生信息
static void totalInforDisplay(); //展示总信息
};
double Student::sum = 0; //初始化静态变量
int Student::stuNum = 0;
Student::Student(string n, double s, string c) {
name = n;
score = s;
className = c;
stuNum++;
sum += score;
}
double Student::getSum() {
return sum;
}
double Student::getAverage() {
return sum / stuNum;
}
void Student::stuDisplay() {
cout << "name:" << name << endl;
cout << "score:" << score << endl;
cout << "className:" << className << endl;
}
void Student::totalInforDisplay() {
cout << "sum:" << getSum() << endl;
cout << "average:" << getAverage()<<endl;
}
int main() {
Student stu[5] = {
Student("Obama",99.0,"1"),
Student("Mike",77.5,"1"),
Student("Jackson",87.0,"2"),
Student("Cherry",93.5,"3"),
Student("Oscar",89.0,"4")
};
for (int i = 0; i < 5; i++) {
stu[i].stuDisplay();
cout << endl;
}
Student::totalInforDisplay();
}
五、重载+,++,=等简单运算符
#include <iostream>
using namespace std;
class Time {
private:
int hour;
int minute;
int second;
public:
Time(); //无参构造函数
Time(int h, int m, int s); //有参构造函数
Time operator + (Time& temp); //重载+运算符
Time operator++(); //重载前置++
Time operator++(int n); //重载后置++
void operator=(Time& temp); //重载赋值运算符
friend ostream& operator <<(ostream& output, Time& temp); //重载输出流
};
Time::Time() {
hour = 0;
minute = 0;
second = 0;
}
Time::Time(int h, int m, int s) :hour(h), minute(m), second(s) {}
Time Time::operator+(Time& temp) {
this->hour += temp.hour;
this->minute += temp.minute;
this->second += temp.second;
return *this;
}
Time Time::operator++() {
second++;
if (second >= 60) {
second = 0;
minute++;
}
if (minute >= 60) {
minute = 0;
hour++;
}
if (hour >= 12) {
hour = 0;
}
return *this;
}
Time Time::operator++(int n) {
Time returnTime(*this);
second++;
if (second >= 60) {
second = 0;
minute++;
}
if (minute >= 60) {
minute = 0;
hour++;
}
if (hour >= 24) {
hour = 0;
}
return returnTime;
}
void Time::operator=(Time& temp) {
this->hour = temp.hour;
this->minute = temp.minute;
this->second = temp.second;
}
ostream& operator<<(ostream& output, Time& temp) {
output << temp.hour << ":" << temp.minute << ":" << temp.second<<endl;
return output;
}
int main() {
Time t1(12, 12, 12);
Time t = t1;
for (int i = 0; i < 60; i++) {
Time temp=t++;
cout << temp;
}
cout << "************" << endl;
t = t1;
for (int i = 0; i < 60; i++) {
Time temp = ++t;
cout << temp;
}
}

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。
更多推荐
所有评论(0)