C language for loop and some exercises

How to print the numbers from 1 to 10 in a for loop, the code is as follows:

int main()
{
    int i = 0;
        初始化  判断   调整
    for ( i = 1; i <= 10; i++)
    {
        if (i == 5)
            continue;
        printf("%d\n", i);
    }
    return 0;
}

The for loop is the loop we use the most, so we must master some basic usage

How to calculate the factorial of n, the code is as follows:

int main()
{
    int n = 0;
    int i = 0;
    int ret = 1;
    scanf("%d", &n); 
    for  (i = 1; i <= n ; i++)
    {
        ret = ret * i;
    }
    printf("ret = %d\n", ret);
    return 0;
}

To find the sum of factorial within 10, the code is as follows:

int main()
{
    int n = 0;
    int i = 0;
    int ret = 1;
    int sum = 0;
    for (n = 1; n <= 10; n++)
    {
        ret = 1;
        for (i = 1; i <= n; i++)
        {
            ret = ret * i;
        }
        sum = sum + ret;
    }
    printf("sum = %d\n", sum);
    return 0;
}

Of course, we can also use another more concise method:

int main()
{
    int n = 0;
    int ret = 1;
    int sum = 0;
    for (n = 1; n <= 10; n++)
    {
        ret = ret * n;
        sum = sum + ret;
    }
    //ret = 1*1=1
    //ret = 1*1*2=2
    //ret = 1*1*2*3=6
    printf("sum = %d\n", sum);
    return 0;
}

How to implement binary search with code:

int main()
{
    int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9,10 };
    int k = 17;

    int sz = sizeof(arr) / sizeof arr[0];
    int left = 0;//左下标
    int right = sz-1;//右下标

    while (left<=right)
    {
        int mid = (left + right) / 2;
        if (arr[mid] > k)
        {
            right = mid - 1;
        }
        else if (arr[mid] < k)
        {
            left = mid + 1;
        }
        else
        {
            printf("找到了\n", mid);
            break;
        }
        if (left > right)
        {
            printf("找不到\n");
        }
    }
    return 0;
}

How to write code to demonstrate that multiple characters move from both ends to converge in the middle:

int main()
{
    //welcome to china!!! 
    //###################
    //w#################!
    //we###############!!
    //...
    //welcome to china!!!
    //
    //char arr[] = "abc";
    //[a,b,c,\0]
    // 0 1 2  3
    //c是4-2
    char arr1[] = "welcome to china!!!";
    char arr2[] = "###################";
    int left = 0;
    //int right = sizeof(arr1) / sizeof(arr1[0]) - 2;//err
    int right = strlen(arr1) - 1;

    while (left<=right)
    {
        arr2[left] = arr1[left];
        arr2[right] = arr1[right];
        printf("%s\n", arr2);
        //休息一秒
        Sleep(1000);
        system("cls");//执行系统命令的一个函数 cls - 清空屏幕
        left++;
        right--;
    }
    printf("%s\n", arr2);
    return 0;
}

Write the code to simulate the user login scenario, and can only log in three times. (It is only allowed to enter the password three times. If the password is correct, you will be prompted to log in. If the password is entered incorrectly three times, the program will exit:

int main()
{
    int i = 0;
    char pd[20] = { 0 };
    for ( i = 0; i < 3; i++)
    {
        printf("请输入密码:>");
        scanf("%s", pd);
        if (strcmp(pd ,"123456") == 0)// == 不能用来比较两个字符串是否相等,应该使用一个库函数strcmp
        {
            printf("登录成功\n");
            break;
        }
        else
        {
            printf("密码错误\n");
        }
    }
    if (i == 3)
        printf("三次密码均错误,退出程序");
    return 0;
}

From the above exercises, we can know some uses of for loops, and some formats of grammar.

2021.1.19
Come on, continue to work hard tomorrow

Guess you like

Origin blog.51cto.com/15080720/2598265