【C++】6-2 交换函数2 分数 10

6-2 交换函数2

分数 10

全屏浏览

切换布局

作者 刘利

单位 惠州学院

根据题目需求,编写一个交换函数Swap。

裁判测试程序样例:

#include <iostream> 
using namespace std;
class pen{
    private:
        string brand;
        string color;
        double price;
    public:
        pen(string _brand="", string _color="", double _price=0)
        {
            brand=_brand;
            color=_color;
            price=_price;
        }
        void print()
        {
            cout<<brand<<" "<<color<<" "<<price<<endl;
        }
};

/* 请在这里填写答案 */

int main()
{
    pen p1("晨光","红色",8.5),p2("得力","黑色",7.5);
    Swap(&p1,&p2);
    p1.print();
    p2.print();
    return 0;    
}

输入样例:

输出样例:

得力 黑色 7.5
晨光 红色 8.5

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

void Swap(pen *p1,pen*p2){
    pen p3;
    p3 = *p1;
    *p1 = *p2;
    *p2 = p3;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很抱歉,我并不是一个能够进行编程的人工智能,无法生成代码。但是我可以提供一些思路和建议。 这道题目需要定义一个 Employee 类,该类包含员工的姓名、编号和薪水信息。另外还需要定义一个 Department 类,该类包含部门的编号、名称和员工信息。在读入员工信息的时候,可以根据员工的部门编号将员工信息添加到相应的部门中。最后计算出平均工资最高的部门即可,并输出该部门的信息以及所在员工的信息。 以下是一个可能的解法: ```python class Employee: def __init__(self, name, id, department_id, salary): self.name = name self.id = id self.department_id = department_id self.salary = salary class Department: def __init__(self, id, name): self.id = id self.name = name self.employees = [] def add_employee(self, employee): self.employees.append(employee) def average_salary(self): total_salary = sum(employee.salary for employee in self.employees) return total_salary / len(self.employees) n = int(input("请输入员工数量:")) departments = {} for i in range(n): name, id, department_id, salary = input().split() employee = Employee(name, id, department_id, int(salary)) if department_id not in departments: department = Department(department_id, "部门" + department_id) departments[department_id] = department else: department = departments[department_id] department.add_employee(employee) max_average_salary = 0 max_department = None for department_id, department in departments.items(): average_salary = department.average_salary() if average_salary > max_average_salary: max_average_salary = average_salary max_department = department print("平均工资最高的部门是:{},平均工资为:{}".format(max_department.name, max_average_salary)) print("部门员工信息如下:") for employee in max_department.employees: print("姓名:{},工号:{},薪水:{}".format(employee.name, employee.id, employee.salary)) ``` 当然,这只是一个简单的参考,实际的实现可能还需要考虑更多细节和异常处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值