c++ day2

  1. 利用函数重载,实现对整形数组的冒泡排序,对浮点型数组的冒泡排序

    复制代码
    #include <iostream>
    #include <cstring>
    #define MAX 5
    using namespace std;
    
    int bubble(int s1[MAX])
    {
        int a[MAX];
        memcpy(a,s1,sizeof(a));
        cout << "整形冒泡排序结果:";
        for(int i = 1;i<MAX;i++)//控制排序趟数
            {
                for(int j = 0;j<MAX-i;j++)//控制两两比较的元素下标
                {
                    if(a[j]>a[j+1])//左边大于右边交换两个元素
                    {
                        int t = a[j];
                        a[j] = a[j+1];
                        a[j+1] = t;
                    }
                }
            }
            for(int i = 0;i<MAX;i++)//循环输出
            {
                cout << a[i] << "\t";
            }
            cout << endl;
    }
    
    
    float bubble(float s2[MAX])
    {
        float a[MAX];
        memcpy(a,s2,sizeof(a));
        cout << "浮点型数组冒泡排序结果:";
        for(int i = 1;i<MAX;i++)//控制排序趟数
            {
                for(int j = 0;j<MAX-i;j++)//控制两两比较的元素下标
                {
                    if(a[j]>a[j+1])//左边大于右边交换两个元素
                    {
                        float t = a[j];
                        a[j] = a[j+1];
                        a[j+1] = t;
                    }
                }
            }
            for(int i = 0;i<MAX;i++)//循环输出
            {
                cout << a[i] << "\t";
            }
            cout << endl;
    }
    
    int main()
    {
        int s1[]={11,44,22,77,33};
        bubble(s1);
        float s2[]={11.1,22.2,55.5,33.3,44.4};
        bubble(s2);
        return 0;
    }
  2. 在堆区申请一个数组的空间,并完成对该数组中数据的输入和输出,程序结束释放堆区空间

    复制代码
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        //输入
        int *p = new int[10];
        cout << "请输入10个整形数据:" ;
        for(int i=0;i<10;i++)
        {
            cin >> p[i];
        }
        cout << endl;
        //输出
        cout << "输入的数据为:";
        for(int i=0;i<10;i++)
        {
            cout << p[i] << " ";
        }
        cout << endl;
        //释放申请的堆空间
        delete []p;
        return 0;
    }
  3. 13题

    复制代码
    #define  array_size(arr)   (sizeof(arr) / sizeof(arr[0]))
  4. 14题

    复制代码
    #include <stdio.h>
     
    int main() {
        int a = 0;  
        a |= (1 << 3);
        a &= ~(1 << 3);
        return 0;
    }
  5. ximd

相关推荐
wljy110 分钟前
二、进制状态转换
linux·运维·服务器·c语言·c++
猫头虎-前端技术13 分钟前
JS 作用域与闭包:从变量提升到闭包陷阱的超详细解析
开发语言·javascript·云计算·bootstrap·ecmascript·openstack·perl
云泽80829 分钟前
笔试算法 -位运算篇(二):从唯一字符到消失数字
c++·算法·位运算
枫叶林FYL30 分钟前
项目十:事件溯源仓储管理系统(WMS)仿真实现
开发语言·python
ʚ希希ɞ ྀ31 分钟前
不同路径|| -- dp
算法
繁华落尽,倾城殇?1 小时前
[C++11] : atomic,nullptr,default/delete,enum class
开发语言·c++·c++11·nullptr·atomic·enum class·default/delete
01_ice1 小时前
C语言数据在内存中的存储
c语言·开发语言
代码村新手1 小时前
C++-二叉搜索树
开发语言·c++
IT 行者1 小时前
SimHash 与 MinHash:相似性计算的双子星算法
算法·hash·比对