C基础 5

1.猜数字游戏

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

	int Meun(){
		printf("==============\n");
		printf("1.开始游戏\n");
		printf("0.结束游戏\n");
		printf("请输入你的选择\n");
		printf("==============\n");
		int choice = 0;
		scanf("%d", &choice);
		return choice;
	}
	void Game(){
		int num = rand() % 100 + 1;//用rand()库函数生成一个随机数,其中rand()%100表示0 到 99 的随机数。
		int tmp = 0;
		//您有10次机会输入数字
		while (tmp < 10){
			printf("请输入您猜的数字\n");
			int to_guess;
			scanf("%d", &to_guess);
			if (to_guess > num){
				printf("您输入的数大了\n");
			}
			else if (to_guess < num){
				printf("您输入的数小了\n");
			}
			else{
				printf("恭喜猜正确了\n");
				return;
			}
			tmp += 1;
		}
	}
	int main(){
		while (1){
		int choice = Meun();
		if (choice == 1){
			Game();
		}
		else if (choice == 0){
			printf("GOODBABY!");
			break;
		}
		else{
			printf("你的输入有误!请重新输入:\n");
		}
	}

	system("pause");
	return 0;
}

2.写代码可以在整型有序数组中查找想要的数字,

找到了返回下标,找不到返回-1.(折半查找)

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#define N  10
int main(){
	int arr[N] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
	int to_find ;
	printf("请输入你要查找的数字:\n");
	scanf("%d", &to_find);
	int left = 0;
	int right = sizeof(arr) / sizeof(arr[0]) - 1;
	while (left <= right){
		int mid = (left + right) / 2;
		if (arr[mid] < to_find){
			left = mid + 1;
		}
		else if (arr[mid] > to_find){
			right = mid - 1;
		}
		else{
			printf("找到了\n");
			printf("下标为%d\n", mid);
			system("pause");
			return 0;

		}
		

	}
	printf("-1\n");

	
	system("pause");
	return 0;
}

3.编写代码模拟三次密码输入的场景。

最多能输入三次密码,密码正确,提示“登录成功”,密码错误,

可以重新输入,最多输入三次。三次均错,则提示退出程序。

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(){
	for (int i = 1; i <= 3; i++){
		char arr[8] = { 0 };
		printf("请输入你的6位密码:");
		scanf("%s", &arr);
		if (strcmp("199801", arr) == 0){
			printf("密码输入正确");
			break;
		}
		else{
			printf("您的输入有误,你还有 %d 次机会输入:\n", 3 - i);
		}

	}
	system("pause");
	return 0;
}

4.编写一个程序,可以一直接收键盘字符,

如果是小写字符就输出对应的大写字符,

如果接收的是大写字符,就输出对应的小写字符,

如果是数字不输出

#define  _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
int main(){

	int ch;
	ch = getchar();
	if (ch >= 'a'&& ch <= 'z'){
	ch = ch - 32;
	printf("%c\n",ch );
	}else if (ch >= 'A'&& ch <= 'Z'){
	ch = ch + 32;
	printf("%c\n",ch );
	}else if (ch >= 0 && ch <= 9);//只能判断出数字0---9.
	else{
	printf("其他字符");
	
	}
	system("pause");
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值