[[C Language Rehabilitation Training-2]]

C language rehabilitation training-2

//Binary ordered array search

int find(int arr1[], int k, int sz)
{
    
    
    int left = 0;
    int right = sz - 1;
    while (left <= right)
    {
    
    
        int mid = (left + right) / 2;
        if (k > arr1[mid])
        {
    
    
            left = mid + 1;
        }
        else if (k < arr1[mid])
        {
    
    
            right = mid - 1;
        }
        else
        {
    
    
            return k;
        }
    }
    if (left > right)
    {
    
    
        return 0;
    }
}

int main()
{
    
    
    int arr1[] = {
    
     1,2,3,4,5,6,7,8,9,11,12,13,23 };
    int a = 0;
    int k = 1;
    int sz = sizeof(arr1) / sizeof(arr1[0]);
    a = find(arr1, k, sz);
    if (a == 0)
    {
    
    
        printf("未找到您所需的数字\n");
    }
    else
    {
    
    
        printf("您想要的数字在第%d位", a);
    }
    return 0;
}

//Accept an integer value (unsigned) and print each bit of it in sequence. For example, input 1234 and output 1 2 3 4
///1234%10 remainder 4. Then continue to modulate 10 and you will get other remainders 3 2 1
//The approach here is good but not particularly good
//Use recursion to convert a complex Let’s simplify the problem first
// We design a function named print
// Suppose the number to be printed is 1234, we first split it into (123) 4 || (12) 3 4 || (1) 2 3 4

void print(int n)
{
    
    
    if (n > 9)
    {
    
    
        print(n / 10);
    }
    printf("%d\n", n % 10);
}
//举个例子就2位 ,递归的意思就是不停的用自身完成简化目的
//我先输入19 第一次进入大于9 ,那么先取一个高位1,再对1 进行操作,
///1进行了一个完整的print操作,最后输出了一个1 .而做完这次之后。下面还
//留有一个printf用来打印9 所以这个方法好
int main()
{
    
    
    unsigned int num = 0;
    scanf("%d", &num);
    print(num);
    return 0;
}

// factorial call of n

int jiecheng(int a)
{
    
    
    int b = 0;
    int sum = 1;
    for (b = 1;b <= a;b++)
    {
    
    
        sum = sum * b;
    }
    return sum;
 }

int main()
{
    
    
    printf("请输入想要输入的数字");
    int a = 0;
    scanf("%d", &a);
    int b = jiecheng(a);
    printf("结果是%d", b);
    return 0;
}

//Recursive ideas about factorial
//Factorial call of n

#include<stdlib.h>
int jiecheng(int a)
{
    
    
    if (a > 1)
    {
    
    
        return a * jiecheng(a - 1);
    }
    else
        return 1;
}

int main()
{
    
    
    printf("请输入想要输入的数字");
    int a = 0;
    int b = 0;
    scanf("%d", &a);
     b = jiecheng(a);
    printf("结果是%d", b);
    return 0;
}  

//bubble sort method

void paixu(int arr[], int sz)
{
    
    
    int i = 0;
    int j = 0;
    int temp = 0;
    for (i = 0;i < sz;i++)
    {
    
    
        for(j=0;j<=i;j++)
            if (arr[j] < arr[i])
            {
    
    
                temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
    }
}

int main()
{
    
    
    int arr1[] = {
    
     9,8,7,6,5,4,3,11,2,45,2,12222,23456 };
    int a = 0;
    int sz = sizeof(arr1) / sizeof(arr1[0]);
    paixu(arr1, sz);
    for (a = 0;a < sz;a++)
    {
    
    
        printf("%d\n", arr1[a]);
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_50965981/article/details/132777177