【c++】pta简单编程题

编程题


7-1 设计一个矩形类Rectangle并创建测试程序(C++)

#include<bits/stdc++.h>
using namespace std;
int main(){
    double a=1,b=1;
    cin>>a>>b;
    cout<<a*b<<endl;
    cout<<(a+b)*2<<endl;
    return 0;
}

7-3 例4-3游泳池改造预算

#include<bits/stdc++.h>
using namespace std;
const double pi=3.1415926;
int main()
{
    int r;
    cin>>r;
    cout<<"Fencing Cost is $"<<pi*(r+3)*70<<endl;
    cout<<"Concrete Cost is $"<<pi*((r+3)*(r+3)-r*r)*20<<endl;
    return 0;
}

7-4 立方体类

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int x,y,z;
    cin>>x>>y>>z;
    cout<<x*y*z<<endl;
    return 0;
}

7-4 立方体类的实现

#include <bits/stdc++.h>
using namespace std;
int main(){
    int x;
    cin>>x;
    cout<<x*x*x<<" "<<x*x*6<<endl;
    return 0;
}

7-1 通过指针,输出对象数组的数据

#include<bits/stdc++.h>
using namespace std;
int main()
{
int number,score;
    for(int i=0;i<5;i++){
cin>>number>>score;
    cout<<number<<" "<<score<<endl;
    }return 0;
}

7-2 计算全班学生C++课程的总成绩和平均成绩

#include<bits/stdc++.h>
using namespace std;
int main()
{
int x,sum;
    for(int i=0;cin>>x&&i<5;i++)
        sum+=x;
        cout<<sum<<endl;
        cout<<sum/5<<endl;
    return 0;
}

7-1 两点间距离计算

#include<bits/stdc++.h>
using namespace std;
int main() {
	for (int ch = 0; cin >> ch && ch != 0;) {
		if (ch == 1) {
			float a, b, z;
			cin >> a >> b;
			z = sqrt((a - b) * (a - b));
			cout << "Distance from Point " << a << " to Point " << b << " is " << z << endl;
		} else if (ch == 2) {
			int a, b, c, d, z;
			cin >> a >> b >> c >> d;
			z = sqrt((a - c) *  (a - c) +  (b - d) * (b - d));
			cout << "Distance from Point" << "(" << a << "," << b << ")" << " to Point(" << c << "," << d << ") is " << z << endl;
		} else {
			int a, b, c, d, e, f, z;
			cin >> a >> b >> c >> d >> e >> f;
			z = sqrt((a - d) * (a - d) + (b - e) * (b - e) + (c - f) * (c - f));
			cout << "Distance from Point" << "(" << a << "," << b << "," << c << ")" << " to Point(" << d << "," << e << "," << f << ") is " << z << endl;
		}
	}
	return 0;
}


7-2 多边形周长计算(继承)

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n, ch;
    cin >> n; // 输入多边形个数
    for (int i = 0; i < n; i++)
    {
        cin >> ch;
        if (ch == 0) // 多边形为边长序列
        {
            int a[100] = {0}, circum = 0, j = 0;
            for (; cin >> a[j] && (a[j] != -1); j++) // 循环读取边长
                circum += a[j]; // 累加边长值到周长
            cout << j << " " << circum << endl; // 输出边长个数和周长
        }
        else if (ch == 1) // 矩形
        {
            int a, b;
            cin >> a >> b; // 读取矩形的长和宽
            cout << "4 " << 2 * (a + b) << endl; // 输出固定个数的边和周长
        }
        else // 其他多边形类型
        {
            int number = 0, side = 0, circum = 0;
            cin >> number >> side; // 读取多边形的边数和边长
            circum = side * number; // 计算周长
            cout << number << " " << circum << endl; // 输出边数和周长
        }
    }
    return 0;
}


7-19 用虚函数计算各种图形的面积

#include<bits/stdc++.h>
using namespace std;
const float PI = 3.14159f;
int main() {
	float sum = 0, r, s, w, h, t, b, bh, ba, th;
	cin >> r >> s >> w >> h >> t >> b >> bh >> ba >> th;
	sum += PI * r * r;
	sum += s * s;
	sum += w * h;
	sum += (t + b) * bh / 2;
	sum += ba * th / 2; 
	printf("%.3f\n", sum );
	return 0;
}


从这里开始就有点复杂了 但是类与对象可以用结构体改写


7-1 图书音像出租管理

#include<bits/stdc++.h>
using namespace std;
struct Publication {
	int type; // 类型,1表示书籍,2表示磁带
	char title[20]; // 名称
	float price; // 原价
	int day; // 租期
	float condition; // 新旧程度,只对书籍有效
	int rented; // 已出租次数,只对磁带有效
};
int main() {
	struct Publication pp[10]; // 出版物数组
	for (int i = 0; cin >> pp[i].type && pp[i].type != 0; i++) { // 循环读取类型,直到为0时结束
		cin >> pp[i].title >> pp[i].price >> pp[i].day; // 读取名称、原价和租期
		if (pp[i].type == 1) //·如果是书籍,读取新旧程度,并存储到结构体中
			cin >> pp[i].condition;
		if (pp[i].type == 2) // 如果是磁带,读取已出租次数,并存储到结构体中
			cin >> pp[i].rented;
		float estimate = 0, cost = pp[i].day * 1.2;  // 估价,租金
		if (pp[i].type == 1) // 计算估价,如果是书籍
			estimate = pp[i].condition * pp[i].price; // 估价为新旧程度乘以原价
		else if (pp[i].type == 2)  //计算估价, 如果是磁带
			estimate = pp[i].price / (1 + pp[i].rented / 3.0); // 估价为原价除以(1+已出租次数/3)
		if (cost > 2 * estimate) // 如果租金超过估价的两倍,需要支付
			printf("%s %.1f %.1f R\n", pp[i].title, cost, estimate * 2);
		else // 否则,只需支付租金
			printf("%s %.1f\n", pp[i].title, cost);
	}
	return 0;
}

7-2 宠物的生长

#include<bits/stdc++.h>
using namespace std;
struct Pet {
	int type; // 类型,1为Cat,2为Dog
	char name[20]; // 姓名
	int length; // 身长
	int weight; // 体重
	int current; // 当前日期
};
int main() {
	struct Pet pt[10]; // 宠物数组,最多10个元素
	int i = 0; // 宠物类型,1为Cat,2为Dog
	for (; cin >> pt[i].type && pt[i].type < 10; i++) // 输入宠物类型
		cin >> pt[i].name >> pt[i].length >> pt[i].weight >> pt[i].current; // 输入姓名,身长,体重,当前日期
	int day = pt[i].type; // 目标日期
	for (int j = 0; j < i; j++) { // 遍历数组
		int diff = day - pt[j].current; // 计算日期差
		if (pt[j].type == 1) { // 如果是Cat
			pt[j].length += diff; // Cat一天身长加1
			pt[j].weight += 2 * diff; // Cat一天体重加2
		} else if (pt[j].type == 2) { // 如果是Dog
			pt[j].length += 2 * diff; // Dog一天身长加2
			pt[j].weight += diff; // Dog一天体重加1
		}
		printf("%s %d %d\n", pt[j].name, pt[j].length, pt[j].weight); // 输出姓名,身长,体重 // 输出姓名,身长,体重
	}
	return 0;
}


7-3 师生信息管理

#include<bits/stdc++.h>
using namespace std;
struct Person {
	int type = 0; // 类型,1为Student,2为Teacher
	int NO = 0; // 编号
	int scores[5] = {0}; // 5门课的成绩(Student)
	int papers[3] = {0}; // 近3年的论文数量(Teacher)
};
int main() {
	struct Person pp[10]; // 人员数组,最多10个元素
	int i = 0; // 数组下标
	for (; cin >> pp[i].type && pp[i].type != 0; i++) { // 输入人员类型
		cin >> pp[i].NO; // 输入编号
		if (pp[i].type == 1) // 如果是Student
			for (int j = 0; j < 5; j++)  // 输入5门课的成绩,并存储到结构体中
				cin >>	pp[i].scores[j];
		else if (pp[i].type == 2) // 如果是Teacher
			for (int j = 0; j < 3; j++) // 输入近3年的论文数量,并存储到结构体中
				cin >> pp[i].papers[j];
	}
	for (int j = 0; j < i; j++) { // 遍历数组
		if (pp[j].type == 1) { // 如果是Student
			int absent = 0, sum = 0, count = 0;; // 缺考的科目数// 已考的科目总分// 已考的科目数
			for (int i = 0; i < 5; i++) // 遍历成绩数组
				if (pp[j].scores[i] == -1) // 如果是-1,表示缺考
					absent++; // 缺考数加一
				else { // 否则,表示已考
					sum += pp[j].scores[i]; // 总分加上该科目分数
					count++; // 已考数加一
				}
			cout << pp[j].NO << ' ' << absent ; // 输出编号和缺考数
			if (count > 0) { // 如果已考数大于0
				float avg = sum / (float)count; // 计算平均分
				printf(" %.1f", avg); // 输出平均分,保留一位小数
			}
			cout << endl;
		} else { // 如果是Teacher
			int total = 0; // 3年论文总数
			for (int i = 0; i < 3; i++) // 遍历论文数组
				total += pp[j].papers[i]; // 总数加上每年的论文数
			cout << pp[j].NO  << ' ' << total << endl; // 输出编号和总数
		}
	}
	return 0;
}


7-4 汽车收费

#include<bits/stdc++.h>
using namespace std;
struct Vehicle {
	int type; // 类型,1为Car,2为Truck,3为Bus
	char NO[20] = {0}; // 编号
	int passengers = 0; // 载客数(Car和Bus)
	int weight = 0; // 重量(Car和Truck)
};
int main() {
	struct Vehicle pv[10]; // 汽车数组,最多10个元素
	for (int i = 0; cin >> pv[i].type && pv[i].type != 0 && i < 10 ; i++) { // 用for循环输入最多10个汽车信息
		cin >> pv[i].NO ; // 输入编号、载客数和重量
		if (pv[i].type == 1) { // 如果是Car
			cin >> pv[i].passengers;
			cin >> pv[i].weight;
			cout << pv[i].NO << " " <<  pv[i].passengers * 8 + pv[i].weight * 2 << endl;// 收费公式为:载客数*8+重量*2
		} else if (pv[i].type == 2) { // 如果是Truck
			cin >> pv[i].weight;
			cout << pv[i].NO << " " << pv[i].weight * 5 << endl; // 收费公式为:重量*5
		} else { // 如果是Bus
			cin >> pv[i].passengers;
			cout << pv[i].NO << " " << pv[i].passengers * 3 << endl; // 收费公式为:载客数*3
		}
	}
	return 0;
}


7-5 饮料的价格

#include<bits/stdc++.h>
using namespace std;
struct Drink {
	int type; // 类型,1为茶,2为咖啡,3为牛奶
	int NO; // 编号
	int amount; // 数量
	float price; // 单价
	int region; // 地区代码(茶)
	int process; // 加工代码(咖啡)
};
int main() {
	struct Drink pd[10]; // 饮品数组,最多10个元素
	int i = 0; // 数组下标
	float cost;
	for (; cin >> pd[i].type && pd[i].type != 0; i++) {
		cin >>  pd[i].NO >> pd[i].amount >>  pd[i].price; // 输入编号、数量和单价
		if (pd[i].type == 1) // 如果是茶,输入地区代码,并存储到结构体中
			cin >> pd[i].region;
		else if (pd[i].type == 2) // 如果是咖啡,输入加工代码,并存储到结构体中
			cin >> pd[i].process;
		if (pd[i].type == 1) // 如果是茶
			if (pd[i].region == 1) // 如果是本地茶
				cost = pd[i].amount * pd[i].price * 1.5;
			else // 如果是其他茶
				cost = pd[i].amount * pd[i].price * 1.2;
		else if (pd[i].type == 2)  // 如果是咖啡
			if (pd[i].process == 1) // 如果是现磨咖啡
				cost = pd[i].amount * pd[i].price * 2;
			else // 如果是其他咖啡
				cost = pd[i].amount * pd[i].price * 1.2;
		else // 如果是牛奶
			cost = pd[i].amount * pd[i].price;
		printf("%d %.1f\n", pd[i].NO, cost); // 输出编号和费用,保留一位小数
	}
	return 0;
}


  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值