Work_5

1.完成猜数字游戏。

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

void Menu(){
    int input;
    printf("***************************
");
    printf("	0:结束游戏
");
    printf("	1:开始游戏
");
    printf("***************************
");
    while (1){
        printf("请输入你的选择:");
        scanf("%d", &input);
        if (input == 1){
            Game();
        }
        else if (input == 0){
            break;
        }
        else {
            printf("你的输入有误!
");
        }
    }

}

void Game(){
    int aim = 0;
    int guess = rand() % 100 + 1;
    printf("请输入你猜的数字:
");
    while (1){
        scanf("%d", &aim);
        if (aim == guess){
            printf("你猜对了!
");
            break;
        }
        else if (aim < guess){
            printf("你猜小了!
");
        }
        else {
            printf("你猜大了!
");
        }
    }
}

int main(){
    menu();
    system("pause");
    return 0;
}

2.写代码可以在整型有序数组中查找想要的数.(折半查找)

1)普通方法

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>

int Find(int arr[], int size, int to_find){
    int i = 0;
    for (i < 0; i < size; i++){
        if (to_find == arr[i]){
            return i;
        }
    }
    return -1;
}

int main(){
    int arr[] = { 1, 2, 3, 4 };
    int size = 0;
    size = sizeof(arr) / sizeof(arr[0]);
    int to_find;
    printf("请输入你要查找的数字:");
    scanf("%d", &to_find);
    int i = Find(arr, size, to_find);
    if (i == -1){
        printf("没找到!
");
    }
    else{
        printf("找到了!
");
        printf("数字下标为:%d", i);
    }
    printf("
");
    system("pause");
    return 0;
}

2)折半查找

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>

int Binaryfind(int arr[], int size, int to_find){
    int low = 0;
    int high = size - 1;
    int mid = 0;
    //[low,high]是一个待查区间
    while (low <= high){
        mid = (low + high) / 2;
        if (to_find < arr[mid]){
            high = mid - 1;
        }
        else if (to_find > arr[mid] ){
            low = mid + 1;
        }
        else {
            return mid;
        }
    }
    return -1;
}

int main(){
    int to_find;
    printf("请输入你要查找的数字:");
    scanf("%d", &to_find);
    int arr[] = { 1, 2, 3, 4, 5, 6, 7, };
    int size = sizeof(arr) / sizeof(arr[0]);
    int i = Binaryfind(arr, size, to_find);
    if (i == -1){
        printf("找不到!
");
    }
    else {
        printf("找到了!
");
        printf("这个数字的下标是:%d", i);
    }
    printf("
");
    system("pause");
    return 0;
}

3.编写代码模拟三次密码输入的场景。
最多能输入三次密码,密码正确,提示“登录成功”,密码错误,
可以重新输入,最多输入三次。三次均错,则提示退出程序。

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>

int Confirm_password(int password[], int input[]){
    if (strcmp(password, input) == 0){
        return 1;
    }
    return 0;
}

int main(){
    char password[100] = "abc123";
    int count = 0;
    char input[100];
    while (count < 3){
        printf("请输入密码:");
        scanf("%s", input);
        int i = Confirm_password(password, input);
        if (i == 1){
            printf("密码正确!
");
            break;
        }
        else {
            printf("密码错误!
");
            count++;
        }
    }
    if (count == 3){
        printf("三次输入错误,已退出程序!");
    }
    printf("
");
    system("pause");
    return 0;
}

4.编写一个程序,可以一直接收键盘字符,
如果是小写字符就输出对应的大写字符,
如果接收的是大写字符,就输出对应的小写字符,
如果是数字不输出。

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>

int main(){    
        printf("请输入:");
    char input;
    while ((input = getchar()) != EOF){
        if (input >= 'a' && input <= 'z'){
            putchar(input - 32);
            putchar('
');
        }
        if (input >= 'A' && input <= 'Z'){
            putchar(input + 32);
            putchar('
');
        }
    }    
    system("pause");
    return 0;
}

EOF :文件结束标记

1)Linux: ctrl + D

2)Window: ctrl + Z 

5.阶乘相加

例:1!+ 2!+ 3!+ ...+ 10!

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>

int Factorial(int input){
    int sum = 1;
    for (int i = 2; i <= input; i++){
        sum = sum * i;
    }
    return sum;
}

int main(){
    int input;
    printf("要求几个数的阶乘之和:");
    scanf("%d", &input);
    int sum = 0;
    for (int i = 1; i <= input; i++){
        sum = sum + Factorial(i);
    }
    printf("%d", sum);
    printf("
");
    system("pause");
    return 0;
}
原文地址:https://www.cnblogs.com/lkimprove/p/10093543.html