【【c语言康复训练-1】】

c语言康复训练-1

//100以内取奇数

int main()
{
    int i = 0;
    while (i < 101)
    {
        if (i % 2 == 1)
        {
            printf("%d\n",i);
        }
        i++;
    }
}

//n的阶乘练习

int main()
{
int a = 0;
int b = 1;
int i = 1;
printf("请输出阶乘数字\n");
scanf("%d", &a);
while (i <= a)
{
b = b*i;
i++;
}
printf("结果是%d", b);
return 0;
}
//1!+2!+…
int main()
{
    int a = 0;
    int b = 1;
    int sum = 0;
    int i = 0;
    printf("请输入你想要输入的数据\n");
    scanf("%d", &a); //接收输入的数值a

    for (i = 1;i <= a;i++)
    {
        b = b * i;
        sum = sum + b;
    }
    printf("您得到的最终结果是:%d\n", sum);
    printf("您得到的结果是:%d\n", b);
    return 0;
}

//二分法查找某数

#include
int main()
{
    int arr[] = { 0,1,2,3,4,5,6,7,8,9,10 };
    int a = 0;
    
    int left = 0;
    
    int i = 0;
    printf("请输入你的数字:");
    scanf("%d", &a);// 输入数字存进a 
    int b = sizeof(arr) / sizeof(arr[0]);
    int right = b - 1;
    while (left <= right)
    {
        int i = (right + left) / 2;
        if (arr[i] > a)
        {
            right = i-1;
        }
        else
            if (arr[i] < a)
            {
                left = i + 1;
            }
            else
            {
                printf("您所输入的数字是:%d", a);
                break;
            }
    }
    if (left > right)
    {
        printf("结果是没找到");
    }
    return 0;
}

//汇聚效果的welcome 设置

int main()
{
    char arr1[] = "welcome to HF !!!";
    char arr2[] = "*****************";
    int left = 0;
    int sz = sizeof(arr2) / sizeof(arr2[0]);
    int right = sz-2;
    while (left <= right)
    {
        arr2[left] = arr1[left];
        arr2[right] = arr1[right];
        left++;
        right--;
        printf("%s\n", arr2);
    }
    return 0;
}

/三次密码实验

int main()
{
    int a = 0;
    char arr[] = { 0 };
    for (a = 0; a < 3; a++)
    {
        printf("输入密码:");
        scanf("%s", arr);
        if (strcmp(arr, "123456") == 0)//等号不能用来比较两个字符串一样,应该用库函数
            //strcmp 函数
        {
            printf("恭喜你解锁成功");
            break;
        }
        else
        {
            printf("恭喜你解锁失败");
        }

    }
    if (a == 3)
    {
        printf("爆炸\n");
    }
    return 0;
}

//辗转相除法求最大公约数

#include
#include
int main()
{
    int a = 0;
    printf("请输入您想要输入的数字a:\n");
    scanf("%d", &a);
    int b = 0;
    printf("请输入您想要输入的数字b:\n");
    scanf("%d", &b);
    int c = 0;
    if (a >b)
    {
        while (a % b)
        {
            c = a % b;
            a = b;
            b = c;
        }
        printf("最小公约数是%d",b );
    }
    else if (a < b)
    {
        while (b % a)
        {
            c = b % a;
            b = a;
            a = c;
        }
        printf("最小公约数是%d",a );
    }    
    else if (a == b)
    {
        printf("最小公约数是%d",a );
    }
    return 0;
}

//判断是否是素数
//偶数肯定不是素数,判断奇数

int main()
{
    int i = 0;
    for (i = 100; i < 200; i++)
    {
        int j = 0;
        for (j = 2; j < i; j++)
        {
            if (i % j == 0)

            {
                break;
            }

        }
        if (j == i)
        {
            printf("%d\n", i);
        }
    }
    return 0;
}

//找一个数组中的最大数

int main()
{
    int arr1[] = { 1,2,3,4,1005,6,7,86,34,12,44,675 };
    //我先创建一个非常简单的数组
    
    int sz = sizeof(arr1) / sizeof(arr1[0]);
    int right = sz - 1;
    int left = 0;
    int a = arr1[left];
    while (left <= right)
    {
        if (arr1[left] > a)
        {
            a = arr1[left];
        }
        left++;
    }
    printf("最大值是:%d", a);
    return 0;
}

//输出简易九九乘法口诀表

int main()
{
    int i = 0;
    int j = 0;
    for (i = 1;i <= 9;i++)
    {
        for (j = 1;j <= i;j++)
        {
            printf("%dx%d=%d  ", i, j, i * j);
        }
        printf("\n");
    }
    return 0;
}

//猜数字游戏

#include
#include
//由系统给出指定的随机值
void menu()
{
    printf("*********************\n");
    printf("*1.开始*****0.结束***\n");
    printf("*********************\n");
}

void game()
{
    int ret = 0;
    int guess = 0;
     ret = rand() % 100 + 1;
    while (1)
    {
        printf("请输入您猜测的数字的数字:\n");
        scanf("%d", &guess);
        if (guess < ret)
        {
            printf("您输入的太小了");
        }
        else if (guess > ret)
        {
            printf("您输入的数字大了");
        }
        else
        {
            printf("恭喜你猜对啦");
            break;
        }
    }
}

int main()
{
    int input = 0;
    srand((unsigned int)time(NULL));
    do
    {
        menu();
        scanf("%d", &input);
        switch (input)
        {
        case 1: game();
            break;
        case 0: 
            printf("游戏结束\n");
            break;
        default: 
            printf("输入非法\n");
            break;
        }
    } while (input);
    return 0;
}

你可能感兴趣的:(萌新的C语言学习,c语言,算法,数据结构)