C语言学习Day21

今天主要学习了数组的算法:

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

//求斐波拉兹数列
void Fibonacci()
{
    int arr[20] = { 1, 1 };

    for (int i = 2; i < 20; i++)
    {
        arr[i] = arr[i - 1] + arr[i - 2];
    }
    for (int i = 0; i < 20; i++)
    {
        if (0 == i % 5)
        {
            printf("\n");
        }
        printf("%d\t", arr[i]);

    }
    printf("\n");
}

//冒泡排序
void MaoPao()
{
    int arr[10];
    //随机数种子
    srand((unsigned)time(NULL));

    //随机输入
    for (int i = 0; i < 10; i++)
    {
        //随机分配10个数到数组中
        arr[i] = rand() % 100;
    }

    //排序
    int mark = 10, flag = 0;    
    for (int i = 0; i < --mark;)
    {
        int x = 0;
        for (int j = 0; j < mark; j++)
        {
            if (arr[j] > arr[j+1])
            {
                int temp = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = temp;
            }
            x++;

        }
        printf("%d\t", x);
        flag++;
    }

    printf("%d\n", flag);
    //输出
    for (int i = 0; i < 10; i++)
    {
        if (0 == i % 5)
        {
            printf("\n");
        }
        printf("%d\t", arr[i]);
    }
    printf("\n");
}

//二维数组行列互换函数
void TwoArray1()
{
    int arr1[2][3] = { {1,2,3},{4,5,6} };
    int arr2[3][2] = { 0 };

    //二维数组行
    for (int i = 0; i < 2; i++) {
        //二位数组列
        for (int j = 0; j < 3; j++) {
            arr2[j][i] = arr1[i][j];
        }
    }

    //输出二维数组
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 2; j++) {
            printf("%d\t", arr2[i][j]);
        }
        printf("\n");
    }

}

//找出二维数组中最大的那个数和他的行列
void TwoArray2()
{
    int arr[3][4] = {0};

    //随机赋值
    srand((unsigned)time(NULL));
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 4; j++) {
            arr[i][j] = rand() % 100;
        }
    }

    //求最大值
    int max = arr[0][0],row,col;
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 4; j++) {
            if (arr[i][j] > max) {
                max = arr[i][j];
                row = i + 1;//用于保存最大值行
                col = j + 1;//用于保存最大值列
            }
        }
    }

    //输出二维数组
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 2; j++) {
            printf("%d\t", arr[i][j]);
        }
        printf("\n");
    }
    printf("最大值为:>%d\n其坐标为:>(%d,%d)", max, row, col);

}

//输出某一行和某一列,其余位置空白
void TwoArray3()
{
    int arr[3][3] = { {1,2,3},{4,5,6},{7,8,9} };
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            if (i == 1 || j == 1) {
                printf("%-5d", arr[i][j]);
            }
            else {
                printf("%-5c",' ');
            }
        }
        printf("\n");
    }
}

//二分法查找
void Half()
{
    int arr[10] = { 1,2,3,76,345,887,34,67 };
    int left = 0, right = 9, mid;
    ////随机赋值
    //srand((unsigned)time(NULL));
    //for (int i = 0; i < 10; i++) 
    //{
    //  arr[i] = rand() % 100;
    //}

    //用插入排序
    for (int i = 1; i < 10; i++)
    {
        int key = arr[i];
        int j = i - 1;
        while ((j >= 0) && (arr[j] > key))
        {
            arr[j + 1] = arr[j];
            j--;
        }
        arr[j + 1] = key;
    }

    for (int i = 0; i < 10; i++)
    {
        printf("%d\t", arr[i]);
    }
    printf("\n");

    //折半查找

    while (left < right)
    {
        mid = (left + right) / 2;
        if (3 == arr[mid])
        {
            printf("3的位置为:>%d\n", mid);
            break;
        }
        else if (3 > arr[mid])
        {
            right = mid;
        }
        else
        {
            left = mid;
        }
    }
}

int main()
{
#if(0)
    Fibonacci();//调用斐波拉兹函数
    MaoPao();//调用冒泡排序
    TwoArray1();//调用二维数组行列互换函数
    TwoArray2();//找出二维数组中最大的那个数和他的行列
    TwoArray3();//输出某一行和某一列,其余位置空白
#endif
    Half();//二分法查找

    return 0;
}
上一篇:day21_内部类丶包装类


下一篇:JavaSE基础入门Day21