助教划水手册(Last Update:2020/05/16)
How To Use
这里记录的是博主助教学习期间课堂上的编程题,有时间的话,我会将解题思路以及各位私下问我问题的一些难点、坑点、易错点整理在对应的每道题下。
考虑到这里要放一个学期的题目,在浏览器中使用 Ctrl + F 搜索题目的关键字,就能快速定位了;
-
函数题编号格式为 "F-题号",编程题格式为 "P-题号"。例如:函数题第 0 题(其实并没有),搜索 F-0 即可,以此类推。(当然你也可以直接搜索题目标题)
-
如果没有搜到要找的题目,那就是还没有这道题,晚些时日再来看吧。毕竟这里的进度必须得比各位的课程进度要慢一些(大家做得快,我也会更新得快一点)
也可以点这里展开目录
函数题: F-1 F-2 F-3 F-4 F-5 F-6 F-7 F-8 F-9 F-10 F-11 F-12 F-13 F-14 F-15 F-16 F-17 F-18 F-19 F-20 F-21 F-22 F-23 F-24 F-25 F-26 F-27 F-28编程题: P-1 P-2 P-3 P-4 P-5 P-6 P-7 P-8 P-9 P-10 P-11 P-12 P-13 P-14 P-15 P-16 P-17 P-18 P-19 P-20 P-21 P-22 P-23 P-24 P-25
博主的思路仅提供参考,不一定是最优解,希望各位不要把这篇文章当成答案集,务必带上思考。如果你对文中的某些点有自己的观点或者更好的方案,欢迎与我交流!
函数题
F-1: 面积计算器(函数重载)
int area(int x,int y) {
return x * y;
}
int area(int x,int y,int z) {
return 2 * (x * y + x * z + y * z);
}
F-2: 引用作函数形参交换两个整数
void Swap(int &a,int &b) {
a = a + b;
b = a - b;
a = a - b;
}
F-3: 带默认形参值的函数
int add(int a,int b = 20,int c = 30) {
return a + b + c;
}
F-4: 最大值函数重载
#include <bits/stdc++.h>
using namespace std;
int myMax(int a,int b) {
return a > b ? a : b;
}
int myMax(int a,int b,int c) {
int t = a > b ? a : b;
return t > c ? t : c;
}
double myMax(double a,double b) {
return a > b ? a : b;
}
F-5: 判断回文
int isPalindrome(const char* str, char* pch) {
int len = strlen(str) - 1;
*pch = str[len/2];
int l = 0,r = len;
while (l <= r) {
if (str[l] != str[r]) {
return 0;
}
l++,r--;
}
return 1;
}
F-6: 字符定位
char *match(char *s, char ch) {
for (int i=0;s[i];i++) {
if (s[i] == ch) {
return s + i;
}
}
return NULL;
}
F-7: 求两个形参的乘积和商
void fun( double a, double b, double *x, double *y ) {
*x = a * b;
*y = a / b;
}
F-8: 使用动态内存分配的冒泡排序
int* bubble_sort(int n) {
int *arr = new int[n];
for (int i=0;i<n;i++) {
cin >> arr[i];
}
for (int i=0;i<n;i++) {
for (int j=n-1;j>i;j--) {
if (arr[j] < arr[j-1]) {
arr[j] = arr[j] + arr[j-1];
arr[j-1] = arr[j] - arr[j-1];
arr[j] = arr[j] - arr[j-1];
}
}
}
return arr;
}
F-9: 使用类计算矩形的面积
void Rectangle::setLength(int l) {
this->length = l;
}
void Rectangle::setWidth(int w) {
this->width = w;
}
int Rectangle::getArea() {
return this->length * this->width;
}
F-10: 类的声明与成员函数的实现--Car 类
void Car::disp_welcomemsg() {
cout << "Welcome to the car world!" << endl;
}
void Car::set_wheels(int n) {
this->m_nWheels = n;
}
int Car::get_wheels() {
return this->m_nWheels;
}
F-11: 圆类的定义
const double PI = acos(-1.0);
class Circle {
private:
double r;
public:
void setR(double r) {
this->r = r;
}
double getR() {
return this->r;
}
double getArea() {
return PI * this->r * this->r;
}
};
F-12: 求 Box 的体积(类的定义与使用)
class Box {
private:
double a,b,c;
public:
void get_value() {
cin >> this->a >> this->b >> this->c;
}
double display() {
cout << this->a * this->b * this->c << endl;
}
};
F-13: 体育俱乐部 I(构造函数)
void Coach::show() {
cout << this->name << " " << this->winRate << "%" << endl;
}
Club::Club(string n1, int y, string n2, int wr):c(n2,wr) {
this->name = n1;
this->year = y;
}
void Club::show() {
cout << this->name << " " << this->year << endl;
this->c.show();
}
F-14: Tree 类的构造函数和成员函数
Tree::Tree() {
this->ages = 1;
}
void Tree::grow(int years) {
this->ages += years;
}
void Tree::age() {
cout << this->ages << endl;
}
F-15: 自定义的学生类
Student::Student(int id,char *name) {
m_id = id;
strcpy(m_name,name);
cout << "Hi!" << m_id << " " << m_name << endl;
}
Student::~Student() {
cout << "Bye!" << m_id << " " << m_name << endl;
}
void Student::print() {
cout << m_id << " " << m_name << endl;
}
F-16: 学生类的构造与析构
class Student {
private:
int num;
string name;
char sex;
public:
Student(int num,string name,char sex): num(num),name(name),sex(sex) {
cout << "Constructor called." << endl;
}
~Student() {
cout << "Destructor called." << endl;
}
void display() {
cout << "num:" << this->num << endl;
cout << "name:" << this->name << endl;
cout << "sex:" << this->sex << endl;
cout << endl;
}
};
F-17: 各省总销量及最高销量(对象数组)
s[i].setCity(city);
s[i].setVolume(volume);
}
double sum = 0;
int max_id = 0;
int fuck_xu_wan_zhen = 0;
for (int i=0;i<n;i++) {
if (i && s[i].getProv() != s[i-1].getProv()) {
cout << s[i-1].getProv() << " sum=";
cout << sum << " max=";
cout << s[max_id].getCity() << ",";
cout << s[max_id].getVolume();
cout << endl;
max_id = i;
sum = 0;
}
sum += s[i].getVolume();
if (s[i].getVolume() > s[max_id].getVolume()) {
max_id = i;
}
}
cout << s[n-1].getProv() << " sum=";
cout << sum << " max=";
cout << s[max_id].getCity() << ",";
cout << s[max_id].getVolume();
return 0;
}
bool cmp(Sale a,Sale b) {
return a.getProv() < b.getProv();
}
void Sale::setProv(string p) {
Sale::prov = p;
}
void Sale::setCity(string c) {
Sale::city = c;
}
void Sale::setVolume(double v) {
Sale::volume = v;
}
string Sale::getProv() {
return Sale::prov;
}
string Sale::getCity() {
return Sale::city;
}
double Sale::getVolume() {
return Sale::volume;
}
F-18: 判断点圆关系
Point::Point(double x_, double y_): x(x_),y(y_) {}
double Point::distance() {
return sqrt((this->x)*(this->x) + (this->y)*(this->y));
}
double Point::distance(const Point &p) {
return sqrt((this->x - p.x)*(this->x - p.x) + (this->y - p.y)*(this->y - p.y));
}
class Circle {
private:
Point ctr;
double r;
public:
Circle(Point ctr, double r): ctr(ctr),r(r) {}
string judge(const Point &p) {
double dis = ctr.distance(p);
if (dis > r) {
return "outside";
} else if (dis < r) {
return "inside";
} else {
return "on";
}
}
};
F-19: 学生信息输入输出
class Student1: public Student {
private:
int age;
string address;
public:
void get_value_1() {
cin >> this->age >> this->address;
}
void display_1() {
cout << "age: " << this->age << endl;
cout << "address: " << this->address << endl;
}
};
F-20: 狗的继承
#include <bits/stdc++.h>
using namespace std;
class Animal {
protected:
int age;
public:
Animal() {}
Animal(int age): age(age) {}
int getAge() {
return this->age;
}
};
class Dog: public Animal {
private:
string color;
public:
Dog(int age,string color) {
this->age = age;
this->color = color;
}
void showInfor() {
cout << "age:" << this->age << endl;
cout << "color:" << this->color << endl;
}
};
F-21: 汽车类的继承
Vehicle::Vehicle(int wheels,float weight) {
this->wheels = wheels;
this->weight = weight;
}
void Vehicle::show() {
cout << "Type:Vehicle" << endl;
cout << "Wheel:" << this->wheels << endl;
cout << "Weight:" << this->weight << "kg" << endl;
}
class Car: public Vehicle {
private:
int load;
public:
Car(int wheels,float weight,int load): Vehicle(wheels,weight){
this->load = load;
}
void show() {
cout << "Type:Car" << endl;
Vehicle::show();
cout << "Load:" << this->load << " persons" << endl;
}
};
F-22: 派生类使用基类的成员函数
class Animal {
private:
int m_nWeightBase;
protected:
int m_nAgeBase;
public:
void set_weight(int w) {
this->m_nWeightBase = w;
}
int get_weight() {
return this->m_nWeightBase;
}
void set_age(int age) {
this->m_nAgeBase = age;
}
};
class Cat: public Animal {
private:
string m_strName;
public:
Cat(string name) {
this->m_strName = name;
}
void print_age() {
cout << this->m_strName << ", age = " << this->m_nAgeBase << endl;
}
};
F-23: 多重继承派生类构造函数
class Graduate: public Teacher, public Student {
private:
float wages;
public:
Graduate(string name, int age, char sex, string title, float score, float wages): \
Teacher(name,age,title),Student("",sex,score),wages(wages) {}
void show() {
cout << "name:" << this->name << endl;
cout << "age:" << this->age << endl;
cout << "sex:" << this->sex << endl;
cout << "score:" << this->score << endl;
cout << "title:" << this->title << endl;
cout << "wages:" << this->wages << endl;
}
};
F-24: 工资与提成
class Salesman {
private:
static int p;
static double amount;
public:
Salesman(string name,double a,double b,double c) {
p++;
amount += a*b*c;
}
static void show() {
cout << "There are " << p
<< " salesmen,sales amount is:" << amount << endl;
}
static double getAver() {
return amount / p;
}
};
int Salesman::p = 0;
double Salesman::amount = 0;
F-25: 计算捐款总量
static float totalMoney;
static void printTotal();
};
float Donator::totalMoney = 0;
void Donator::printTotal() {
cout << "total:" << Donator::totalMoney << endl;
}
void Donator::setName(string _name) {
this->name = _name;
}
void Donator::setMoney(float _money) {
this->money = _money;
Donator::totalMoney += _money;
}
string getMaxName(Donator dt[],int n) {
int id = 0;
for (int i=0;i<n;i++) {
if (dt[i].getMoney() > dt[id].getMoney()) {
id = i;
}
}
return dt[id].getName();
}
F-26: 班级与班主任类(对象成员、初始化列表)
class Class {
private:
int grade;
int id;
Teacher t;
void showInfo(int grade, int id, string name, string course) {
cout << "grade:" << grade << " ";
cout << "class no:" << id << ",";
cout << "head teacher:" << name;
cout << "(" << course << ")" << endl;
}
public:
Class() {
cout << "new class created." << endl;
}
Class(int grade, int id) {
this->showInfo(grade,id,"unknow","chinese");
}
Class(int grade, int id, string name) {
this->showInfo(grade,id,name,"chinese");
}
Class(int grade, int id, string name, string course) {
this->showInfo(grade,id,name,course);
}
};
F-27: 类的声明和成员函数的实现--this 指针
class Car {
private:
int m_nWheels;
public:
void disp_welcomemsg() {
cout << "Welcome to the car world!" << endl;
}
void set_wheels(int n) {
this->m_nWheels = n;
}
int get_wheels() {
return this->m_nWheels;
}
};
F-28: 学生排名表(析构函数)
for (int i=0;i<count;i++) {
for (int j=i+1;j<count;j++) {
if (pS[j]->getRank() < pS[i]->getRank()) {
swap(pS[i],pS[j]);
}
}
delete pS[i];
}
编程题
P-1: 测试 c++
#include <iostream>
int main() {
int n;
std::cin >> n;
int num[n];
for (int i=0;i<n;i++) {
std::cin >> num[i];
}
for (int i=n-1;i>=0;i--) {
std::cout << num[i];
}
std::cout << std::endl;
return 0;
}
P-2: 计算三角形面积
#include <iostream>
#include <cmath>
int main() {
double a,b,c;
std::cin >> a >> b >> c;
if (a + b > c && a + c > b && b + c > a) {
double p = (a+b+c)/2;
std::cout << sqrt(p*(p-a)*(p-b)*(p-c)) << std::endl;
} else {
std::cout << "No" << std::endl;
}
return 0;
}
P-3: 问候
#include <iostream>
#include <string>
int main() {
std::string name;
std::cin >> name;
std::cout << "Hello!What's your name?" << std::endl;
std::cout << name;
std::cout << ",Welcome to learn OOP using C++!" << std::endl;
return 0;
}
P-4: 求交错序列前 N 项和
#include <iostream>
#include <iomanip>
int main() {
int n;
std::cin >> n;
double ans = 0,x = 1;
for (int i=1;i<=n;i++) {
ans += x * i / (2*i-1);
x *= -1.0;
}
std::cout << std::fixed << std::setprecision(3) << ans << std::endl;
return 0;
}
P-5: 特殊 a 串数列求和
#include <iostream>
int main() {
int a,n;
std::cin >> a >> n;
int ans = 0;
for (int i=n;i>=0;i--) {
ans += a*i;
a *= 10;
}
std::cout << "s = " << ans << std::endl;
return 0;
}
P-6: 统计一行文本的单词个数
#include <iostream>
#include <string>
int main() {
std::string str;
int ans = 0;
while (std::cin >> str) {
ans++;
}
std::cout << ans << std::endl;
return 0;
}
P-7: 鸿鸿哥分钱
#include <bits/stdc++.h>
using namespace std;
const int N = 105;
bool isPrime[N];
int Prime[N],tot;
void getPrime() {
memset(isPrime,true,sizeof(isPrime));
for (int i=2;i<N;i++) {
if (isPrime[i]) {
Prime[tot] = i;
tot++;
}
for (int j=0;j<tot && i*Prime[j] < N;j++) {
isPrime[i*Prime[j]] = false;
if (i % Prime[j] == 0) {
break;
}
}
}
}
int main() {
getPrime();
int x,y;
cin >> x >> y;
for (int i=x;i<=y;i++) {
if (i & 1) continue;
for (int j=0;j<tot && Prime[j] <= i/2;j++) {
if (isPrime[i-Prime[j]]) {
cout << i << "=" << Prime[j] << "+" << i-Prime[j] << endl;
break;
}
}
}
return 0;
}
P-8: 估值一亿的 AI 核心代码
这道题好恶心,可把我给坑惨了 ;自我感觉我的代码时间复杂度以及健壮性都还算可以,一些测试点没有考虑到的情况我也考虑在里面了。
#include <bits/stdc++.h>
using namespace std;
struct SuperAI {
string str;
int pos;
bool come_up_can;
bool changed;
inline void init() {
str += ' ';
pos = 0;
while (str[pos] == ' ') pos++;
changed = false;
come_up_can = false;
}
inline void readLine() {
getline(cin,str);
cout << str << endl;
init();
}
inline bool isText(char ch) {
return isalpha(str[pos]) || isdigit(str[pos]);
}
inline char toAvailableCase(char ch) {
if (ch == '?')
ch = '!';
else if (ch != 'I')
ch = tolower(ch);
return ch;
}
string getNextWord() {
string word = "";
char separate = 0;
while (str[pos] && !isText(str[pos])) {
separate = toAvailableCase(str[pos]);
pos++;
if (separate != ' ')
return word + separate;
}
while (str[pos] && isText(str[pos])) {
word += toAvailableCase(str[pos]);
pos++;
}
if (word == "could" || word == "can") {
come_up_can = true;
string next = getNextWord();
come_up_can = false;
if (!changed && next == " you")
word = "I " + word;
else
word = word + next;
changed = false;
} else if (word == "I" || word == "me") {
if (come_up_can)
changed = true;
word = "you";
}
if (word != "" && separate)
word = separate + word;
return word;
}
void speak() {
string next = "";
string output = "";
while ((next = getNextWord()) != "") {
output += next;
}
cout << "AI: " << output << endl;
}
} AI;
int main() {
int n;
cin >> n;
getchar();
while (n--) {
AI.readLine();
AI.speak();
}
return 0;
}
P-9: 实数排序
#include <bits/stdc++.h>
using namespace std;
void QuickSort(double *input,int l,int r) {
int low = l,hight = r;
if (low < hight) {
double key = input[low];
while (low != hight) {
while (hight > low && input[hight] <= key)
hight--;
input[low] = input[hight];
while (low < hight && input[low] >= key)
low++;
input[hight] = input[low];
}
input[low] = key;
QuickSort(input,l, low - 1);
QuickSort(input, low + 1, r);
}
}
int main() {
int n;
cin >> n;
double *input = new double[n];
for (int i=0;i<n;i++) {
cin >> input[i];
}
QuickSort(input,0,n-1);
for (int i=0;i<n;i++) {
if (i)
cout << " ";
cout << fixed << setprecision(2) << input[i];
}
return 0;
}
P-10: 字符串的连接
#include <bits/stdc++.h>
using namespace std;
int main() {
string a,b;
getline(cin,a);
getline(cin,b);
cout << a << b << endl;
return 0;
}
P-11: 使用函数删除字符串中的字符
#include <bits/stdc++.h>
using namespace std;
void delchar(char* str, char c) {
cout << "result: ";
for (int i=0;str[i];i++) {
if (str[i] != c) {
putchar(str[i]);
}
}
cout << endl;
}
int main() {
int repeat;
char str[5000],c;
cin >> repeat;
while (repeat--) {
getchar();
cin.getline(str,5000);
cin >> c;
delchar(str,c);
}
return 0;
}
P-12: 学生的平均成绩
#include <bits/stdc++.h>
using namespace std;
struct Student {
int id;
string name;
double score[3];
void getInfo() {
cin >> id >> name >> score[0] >> score[1] >> score[2];
}
double printInfo() {
double average = (score[0] + score[1] + score[2])/3;
cout << setw(5) << id;
cout << setw(5) << name;
cout << setw(5) << fixed << setprecision(1) << average;
cout << endl;
}
} student[5];
int main() {
for (int i=0;i<5;i++) {
student[i].getInfo();
student[i].printInfo();
}
return 0;
}
P-13: 找出总分最高的学生
#include <bits/stdc++.h>
using namespace std;
struct StuInfo {
string id;
string name;
int score;
StuInfo() {
score = 0;
}
};
int main() {
int n;
cin >> n;
StuInfo student[n];
int excellent_id = 0;
for (int i=0;i<n;i++) {
int a,b,c;
cin >> student[i].id >> student[i].name >> a >> b >> c;
student[i].score = a + b + c;
if (student[excellent_id].score < student[i].score)
excellent_id = i;
}
cout << student[excellent_id].name << " ";
cout << student[excellent_id].id << " ";
cout << student[excellent_id].score << endl;
return 0;
}
P-14: 一帮一
#include <bits/stdc++.h>
using namespace std;
struct StuInfo {
int rank;
string name;
};
int main() {
int n;
deque<StuInfo> student[2];
cin >> n;
for (int i=0;i<n;i++) {
int id;
string name;
cin >> id >> name;
student[id].push_back(StuInfo{i,name});
}
while (!student[0].empty()) {
int a = student[1].front().rank < student[0].front().rank;
int b = a ^ 1;
cout << student[a].front().name << " " << student[b].back().name << endl;
student[a].pop_front();
student[b].pop_back();
}
return 0;
}
P-15: 领装备
#include <bits/stdc++.h>
using namespace std;
struct Data {
string id;
string star;
};
int main() {
int n;
cin >> n;
Data data[n];
for (int i=0;i<n;i++) {
string id,star;
int card;
cin >> id >> card >> star;
data[card-1] = {id,star};
}
int m;
cin >> m;
for (int i=0;i<m;i++) {
int card;
cin >> card;
cout << data[card-1].id << " " << data[card-1].star << endl;
}
return 0;
}
P-16: 查找单价最高和最低的书籍
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
struct Book {
string name;
double price;
Book(bool type) {
price = type?INF:-INF;
}
};
int main() {
int n;
cin >> n;
Book highest(0),lowest(1);
for (int i=0;i<n;i++) {
getchar();
string name;
getline(cin,name);
double price;
cin >> price;
if (price > highest.price) {
highest.name = name;
highest.price = price;
}
if (price < lowest.price) {
lowest.name = name;
lowest.price = price;
}
}
cout << "highest price: " << fixed << setprecision(1) << highest.price << ", " << highest.name << endl;
cout << "lowest price: " << fixed << setprecision(1) << lowest.price << ", " << lowest.name << endl;
return 0;
}
P-17: 立方体类的实现
#include <bits/stdc++.h>
using namespace std;
class Box {
private:
float a,volume,area;
public:
void seta(float a) {
this->a = a;
}
void getvolume() {
this->volume = this->a * this->a * this->a;
}
void getarea() {
this->area = this->a * this->a * 6;
}
void disp() {
cout << this->volume << " " << this->area << endl;
}
};
int main( ){
float ab;
cin>>ab;
Box obj;
obj.seta( ab );
obj.getvolume( );
obj.getarea( );
obj.disp( );
return 0;
}
P-18: 设计一个矩形类 Rectangle 并创建测试程序(C++)
#include <bits/stdc++.h>
using namespace std;
class Rectangle {
private:
double width;
double height;
public:
Rectangle() {
}
Rectangle(double width,double height) {
this->width = width;
this->height = height;
}
double getArea() {
return this->width * this->height;
}
double getPerimeter() {
return (this->width + this->height) * 2.0;
}
};
int main() {
double width,height;
cin >> width >> height;
Rectangle rectangle(width,height);
cout << rectangle.getArea() << endl;
cout << rectangle.getPerimeter() << endl;
return 0;
}
P-19: 复数类的操作
#include <bits/stdc++.h>
using namespace std;
class Complex {
private:
double real;
double image;
public:
Complex(double real, double image) {
this->real = real;
this->image = image;
}
Complex NOT() {
double real = this->real * -1;
double image = this->image * -1;
Complex ret(real,image);
return ret;
}
void Print() {
cout << "(" << this->real << ", " << this->image << ")" << endl;
}
friend Complex add(Complex a,Complex b);
};
Complex add(Complex a,Complex b) {
double real = a.real + b.real;
double image = a.image + b.image;
Complex ret(real,image);
return ret;
}
int main() {
double real,image;
cin >> real >> image;
Complex c1(real,image);
cin >> real >> image;
Complex c2(real,image);
Complex sum1 = add(c1,c2);
sum1.Print();
Complex sum2 = add(c1,c2.NOT());
sum2.Print();
c2.Print();
return 0;
}
P-20: 宿舍谁最高?
#include <bits/stdc++.h>
using namespace std;
class Student {
private:
int id;
string name;
int height;
int weight;
public:
void init(int id, string name, int height, int weight) {
this->id = id;
this->name = name;
this->height = height;
this->weight = weight;
}
int getID() {
return this->id;
}
bool operator < (const Student &p) const {
if (id == p.id)
return height > p.height;
return id < p.id;
}
void Print() {
cout << setw(6) << setfill('0') << id << " "
<< name << " "
<< height << " "
<< weight << endl;
}
};
int main() {
int n;
cin >> n;
Student student[n];
for (int i=0;i<n;i++) {
int id,height,weight;
string name;
cin >> id >> name >> height >> weight;
student[i].init(id,name,height,weight);
}
sort(student,student+n);
int last = -1;
for (int i=0;i<n;i++) {
if (student[i].getID() != last) {
student[i].Print();
last = student[i].getID();
}
}
return 0;
}
P-21: 图书音像出租管理
#include <bits/stdc++.h>
using namespace std;
class Publication {
protected:
string title;
float price;
int day;
public:
virtual void display()=0;
};
class Book:public Publication {
private:
float oldRate;
public:
Book(string title,float price,int day,float oldRate) {
this->title = title;
this->price = price;
this->day = day;
this->oldRate = oldRate;
}
void display() {
float rent = 1.2 * day;
float eval = oldRate * price;
cout << title << " " << fixed << setprecision(1) << rent;
if (rent > eval * 2) {
cout << " " << fixed << setprecision(1) << eval * 2 << " R";
}
cout << endl;
}
};
class Tape:public Publication {
private:
float rentedTimes;
public:
Tape(string title,float price,int day,float rentedTiems) {
this->title = title;
this->price = price;
this->day = day;
this->rentedTimes = rentedTiems;
}
void display() {
float rent = 1.2 * day;
float eval = price / (1 + rentedTimes/3);
cout << title << " " << fixed << setprecision(1) << rent;
if (rent > eval * 2) {
cout << " " << fixed << setprecision(1) << eval * 2 << " R";
}
cout << endl;
}
};
int main() {
Publication *pp[10];
int type;
int tot = 0;
while (cin >> type,type) {
string a;
float b;
int c;
float d;
cin >> a >> b >> c >> d;
if (type == 1) {
Book *book = new Book(a,b,c,d);
pp[tot] = book;
} else {
Tape *tape = new Tape(a,b,c,d);
pp[tot] = tape;
}
tot++;
}
for (int i=0; i<tot; i++) {
pp[i]->display();
}
return 0;
}
P-22: 体育俱乐部积分管理
#include <bits/stdc++.h>
using namespace std;
class Ball {
protected:
string opponent;
public:
virtual void display() = 0;
};
class Basketball: public Ball {
private:
int score;
public:
Basketball(string opponent,int myScore,int enemyScore) {
this->opponent = opponent;
if (myScore > enemyScore) {
this->score = 2;
} else if (myScore < enemyScore) {
this->score = 1;
} else {
this->score = 0;
}
}
void display() {
cout << this->opponent << " " << this->score << endl;
}
};
class Football: public Ball {
private:
int score;
public:
Football(string opponent,int myScore,int enemyScore) {
this->opponent = opponent;
if (myScore > enemyScore) {
this->score = 3;
} else if (myScore == enemyScore) {
this->score = 1;
} else {
this->score = 0;
}
}
void display() {
cout << this->opponent << " " << this->score << endl;
}
};
class Volleyball: public Ball {
private:
int score;
public:
Volleyball(string opponent,int myScore,int enemyScore) {
this->opponent = opponent;
if (myScore == 3) {
if (enemyScore == 0 || enemyScore == 1) {
this->score = 3;
} else if (enemyScore == 2) {
this->score = 2;
}
} else if (myScore == 2 && enemyScore == 3) {
this->score = 1;
} else {
this->score = 0;
}
}
void display() {
cout << this->opponent << " " << this->score << endl;
}
};
int main() {
Ball *pb[20];
int tot = 0;
int type;
char ch;
while (cin >> type,type) {
string opponent;
int myScore,enemyScore;
cin >> opponent >> myScore >> ch >> enemyScore;
if (type == 1) {
pb[tot] = new Basketball(opponent,myScore,enemyScore);
} else if (type == 2) {
pb[tot] = new Football(opponent,myScore,enemyScore);
} else if (type == 3) {
pb[tot] = new Volleyball(opponent,myScore,enemyScore);
}
tot++;
}
for (int i=0;i<tot;i++) {
pb[i]->display();
}
return 0;
}
P-23: 选择法排序
#include <bits/stdc++.h>
using namespace std;
bool cmp(int a, int b) {
return a > b;
}
int main() {
int n;
cin >> n;
int num[n];
for (int i=0;i<n;i++) {
cin >> num[i];
}
sort(num,num+n,cmp);
for (int i=0;i<n;i++) {
if (i)
cout << " ";
cout << num[i];
}
cout << endl;
return 0;
}
P-24: 三个整数之和
#include <bits/stdc++.h>
using namespace std;
int main() {
int a,b,c;
cin >> a >> b >> c;
cout << "sum=" << a+b+c << endl;
return 0;
}
P-25: 抽象基类 Shape 派生 3 个类
#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
class Shape {
public:
virtual double getArea() = 0;
};
class Circle: public Shape {
private:
double r;
public:
Circle(double r): r(r) {}
double getArea() {
return PI * r * r;
}
};
class Rectangle: public Shape {
private:
double h,w;
public:
Rectangle(double h, double w): h(h),w(w) {}
double getArea() {
return h * w;
}
};
class Triangle: public Shape {
private:
double h,a;
public:
Triangle(double h,double a): h(h),a(a) {}
double getArea() {
return 0.5 * a * h;
}
};
int main() {
Shape *shape[3];
double r;
cin >> r;
shape[0] = new Circle(r);
double h,w;
cin >> h >> w;
shape[1] = new Rectangle(h,w);
double a;
cin >> h >> a;
shape[2] = new Triangle(h,a);
for (int i=0; i<3; i++) {
cout << fixed << setprecision(2) << shape[i]->getArea() << endl;
}
return 0;
}

浙公网安备 33010602011771号