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

相关推荐
倒头就睡的小比特3 天前
算法竞赛C++常用的STL
c++·算法
weilx12343 天前
C++笔记-文件IO-<fcntl.h>
c++
小羊没烦恼!3 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
伞伞悦读3 天前
【第38期】Python 模块与包详解:import、from、模块搜索路径、包结构和 __init__
开发语言·python
猎头南楼3 天前
知识社区推荐系统实践:新用户冷启动与长短期兴趣建模的挑战 资深推荐算法工程师
人工智能·深度学习·算法·机器学习
Smileyqp沛沛3 天前
前端?C++ ?较大差异基础罗列
c++·基础·前端转c++
C语言小火车3 天前
C/C++ 为什么需要编译器?
开发语言·c++
旖旎夜光3 天前
力控面试题 01.01: 判定字符是否唯一(位运算) —— 题解
c++·学习·算法·leetcode·力控
wzdark3 天前
大规模并行计算中的负载均衡算法研究4
算法
吞下星星的少年·-·3 天前
C++ 萌新语法入门篇
c++·算法比赛