21年5月29小题目


void Match() {   for (int A = 'X'; A <= 'Z'; A++)   {     for (int B = 'X'; B <= 'Z'; B++)     {       for (int C = 'X'; C <= 'Z'; C++)       {         if (A != 'X' && C != 'X' && C != 'Z' && A != B && A != C && B != C)         {           printf("A 与 %c B 与 %c C 与 %c", A, B, C);         }       }     }   } } int main() {   Match();   return 0; }

冒泡排序的优化

//void Swap(int* a, int* b)
//{
//    assert(a != nullptr && b != nullptr);
//    int tem = *a;
//    *a = *b;
//    *b = tem;
//}

void Swap(int& a, int& b)   //这里不是地址   是取别名  C++的引用
{
    int tem = a;
    a = b;
    b = tem;
}


//冒泡排序的优化
void Sort(int* ar, int n)
{
    assert(ar != nullptr);
    
    for (int i = 0; i < n-1; i++)
    {
        bool flag = true;
        for (int j = 0; j < n-i-1; j++)
        {
            if (ar[j] > ar[j+1])
            {
                //Swap(&ar[j],&ar[j+1]);
                Swap(ar[j],ar[j+1]);
                flag = false;
            }
        }
        if (flag)
        {
            break;
        }
    }
}

3

//题目//不定义变量(就是不实例化),计算出偏移量

//答案:使用offset宏
#include<stdio.h>
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE*)0) -> MEMBER)
/*
1     ( (TYPE *)0 )  
    0地址强制 "转换" 为 TYPE结构类型的指针;

2     ((TYPE *)0)->MEMBER   
    访问TYPE结构中的MEMBER数据成员;

3     &( ( (TYPE *)0 )->MEMBER)
    取出TYPE结构中的数据成员MEMBER的地址;

4     (size_t)(&(((TYPE*)0)->MEMBER))
    结果转换为size_t类型。

*/


struct Node
{
    short sx;
    char str[3];
    double dx;
    char strb[7];
    int age;
    long int num;

};


int main()
{
    Node x;
    int dist = (int)((char*)&x.age - (char*)&x);
    printf("%d 
", dist);

    printf("%d 
", offsetof(struct Node, age));


    return 0;
}
Linux学习笔记
原文地址:https://www.cnblogs.com/zealwang/p/14825485.html