C++完成整型数组中求max或min(运用bool类型,命名空间,输入输出,函数调用的知识)

运用bool类型,命名空间,输入输出,函数调用的知识 

#include<stdlib.h>
#include<iostream>
using namespace std;
//定义函数getMaxOrMin
int getMaxOrMin(int *arr,int count,bool isMax)
{
    int temp=arr[0];
    for(int i=1;i<count;i++)
    {
        if(isMax)//进行bool判断
        {
            if(temp<arr[i])
            {
              temp=arr[i];//结果为true取最大值
            }
        }
        else
        {
            if(temp>arr[i])
            {
              temp=arr[i];//结果为false取最小值
            }
        }
    }
    return temp;
}
int main(void)//调用函数getMaxOrMin
{
    int arrl[4]={3,5,1,7};
    bool isMax=false;
    cin>>isMax;
    cout<<getMaxOrMin(arrl,4,isMax)<<endl;
    system("pause");
    return 0;

}

整个过程用的C++编写,我们来验证一下程序对不对,

输入0,判断为false,取最小值1

输入1,判断为true,取最大值7

希望能帮到大家,问你们要一个赞,你们会给吗,谢谢大家
版权声明:本文版权归作者(@攻城狮小关)和CSDN共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
大家写文都不容易,请尊重劳动成果~ 
交流加Q:1909561302
博客园地址https://www.cnblogs.com/guanguan-com/

好的,以下是使用选择法排序的C++语音代码实现: ```c++ #include <iostream> #include <vector> #include <algorithm> using namespace std; struct Employee { string name; int performance; }; bool cmp(Employee a, Employee b) { return a.performance > b.performance; } void selectionSort(vector<Employee>& employees) { int n = employees.size(); for (int i = 0; i < min(10, n); i++) { int max_idx = i; for (int j = i + 1; j < n; j++) { if (employees[j].performance > employees[max_idx].performance) { max_idx = j; } } if (max_idx != i) { swap(employees[i], employees[max_idx]); } } } int main() { int n; cout << "请输入员工人数:"; cin >> n; vector<Employee> employees(n); int total_performance = 0; for (int i = 0; i < n; i++) { cout << "请输入第" << i + 1 << "个员工的姓名和业绩:"; cin >> employees[i].name >> employees[i].performance; total_performance += employees[i].performance; } double avg_performance = 1.0 * total_performance / n; cout << "所有员工的平均业绩为:" << avg_performance << endl; selectionSort(employees); cout << "前10名优秀员工为:" << endl; for (int i = 0; i < min(10, n); i++) { cout << employees[i].name << " " << employees[i].performance << endl; } return 0; } ``` 这段代码和之前的代码实现是一样的,只是加入了语音输出的功能。程序会提示你输入员工人数和每个员工的姓名和业绩,然后计算平均业绩,并按照业绩从高到低排序,选出前10名优秀员工,并输出他们的姓名和业绩。 如果你在运行程序时使用了支持语音输出的终端,程序会通过语音告诉你需要输入的信息,并在排序完成后语音输出前10名优秀员工的信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

攻城狮小关

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值