猜数字游戏

1.猜数字游戏:

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


int main(void){

	int a , b , c;
	srand((unsigned int)time(NULL));
	a = rand() % 100;
	printf("猜数字游戏开始:请输入100以内的数字:\n");

	while (1){
		
		scanf("%d", &b);
		if (b < a) {
			printf("低了\n");
		}
		else if (b > a) {
			printf("高了\n");
		}
		else
			printf("恭喜你猜对了!\n");
	}

	system("pause");
	return 0;
}

运行结果

:


2.写代码可以在整型有序数组中查找想要的数字,
找到了返回下标,找不到返回-1.(折半查找)

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

int binarysearch(int arr[], int right, int Tofind);

int main(void) {
	int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
	int b = 0;
	int arr_size = sizeof(arr) / sizeof(arr[0]) - 1;
	
	printf("请输入你想查找的数: \n");
	scanf("%d", &b);

	int ret = binarysearch(arr, arr_size, b);
	printf("下标是: %d\n", ret);

	system("pause");
	return 0;
}
int binarysearch(int arr[], int right, int Tofind) {
	int left = 0;
	int  mid = (left + right) / 2;
	while (left <= right) {
		mid = (left + right) / 2;
		if (arr[mid] == Tofind) {
			return mid;
		}
		else if (arr[mid] > Tofind) {
			right = mid - 1;
		}
		else {
			left = mid + 1;
		}
	}
	if (left > right) {
		return -1;
	}
}

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

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


int main(void){
	
	int key = 123;
	int i = 1;
	int a = 0;
	printf("请输入3位密码:");
	scanf("%d", &a);
	if (a == key) {
		printf("登陆成功!\n");
	}
	else while(a != key){
		printf("您的输错%d次!请重新输入3位密码:\n" , i);
		scanf("%d", &a);
		if (a == key) {
			printf("登陆成功!\n");
			break;
		}
		i++;
		if (i > 2)
			break;
	}
	

	system("pause");
	return 0;
}

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

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


int main(void){
	int ch = '0';
	while (1) {
		ch = getchar();
		if ( ch >= 'a' && ch <= 'z') {
			printf("%c", ch - 32);
		}
		else if(ch >= 'A' && ch <= 'Z') {
			printf("%c", ch + 32);
		}
		else if (ch >= '0' && ch <= '9') {
			continue;
		}

	}

	system("pause");
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值