今日收获

今日份编程和一些问题的总结
1.完成猜数字游戏。
在解决此问题之前,我们先分析一下会用到rand,srand,time等函数来产生随机数。
系统在调用rand()之前都会自动调用srand(),如果用户在rand()之前曾调用过srand()给参数seed指定了一个值,那么 rand()就会将seed的值作为产生伪随机数的初始值;而如果用户在rand()前没有调用过srand(),那么系统默认将1作为伪随机数的初始 值。如果给了一个定值,那么每次rand()产生的随机数序列都是一样的~~
所以为了避免上述情况的发生我们通常用srand((unsigned)time(0))或者srand((unsigned)time(NULL))来 产生种子。

#define _CRT_SECURE_NO_WARNINGS
#include"stdio.h"
#include"stdlib.h"
#include"time.h"
menu(){
	printf("1.开始游戏");
	printf("2.退出游戏");
}
void game_playing(){
	int random = rand() % 100;  //产生0到99之间的随机数
	int to_guess;
	while (1){
		printf("please the number you will guess:\n");
		scanf("%d", &to_guess);
		if (to_guess < random){
			printf("猜低了!");
		}
		else if (to_guess>random){
			printf("猜高了!");
		}
		else
			printf("猜中了");
	}
}
int main(){
	int a = 0;
	char ch = 0;//初始化选择
	while ((ch = getchar(a)) != EOF){     //保证选择不对接着选
		srand((unsigned int)time(NULL));
		printf("please choose:\n");
		scanf("%d", &a);
		switch (a){
		case 0:
			printf("退出游戏");
			break;
		case 1:
			game_playing();
			break;
		default:
			printf("输入错误,请重新输入");
			break;
		}
	}
}

2.写代码可以在整型有序数组中查找想要的数字,
找到了返回下标,找不到返回-1.
见到此问题我们就会想到折半查找
将要查找的数与中间值比较

#include"stdio.h"
#include"stdlib.h"
int BinarySearch(int a[], int left, int right, int key){
	int mid = (left + right) / 2;   //相当于右移一位  int mid=(left+(right-left))>>1
	while(left<=right){
	if (a[mid] > key){
		right = mid - 1;
	}
	else if (a[mid] < key){
		left = mid + 1;

	}
	else
		return mid;
}
return -1;
}
main(){
	int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
	int left = 0;
	int length = sizeof(a) / sizeof(a[0]);
	int right = length - 1;
	int key = 5;
	int b=BinarySearch(a,left,right,key);
	printf("%d\n", b);
	system("pause");
}

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

#define _CRT_SECURE_NO_WARNINGS
#include"stdio.h"
#include"stdlib.h"

main(){
	int i = 0;
	char password[11] = "";
	for (i = 0; i < 3; i++){
		printf("please input the password:\n");
		scanf("%s", password);   //字符串不需要&符号
		if (strcmp(password, "16408061110") == 0)
			break;
		
	}

		printf("登录成功");
	
	if(i==3){
		printf("登录失败");
	}
	system("pause");

}

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

分析:首先需要保证一直接受键盘字符则需要满足此条件ch = getchar()) != EOF,并且还会用到getchar(),putchar()两个函数,它们分别表示读取用户从键盘输入的单个字符和向终端输出字符

#include"stdio.h"
#include"stdlib.h"
main(){
	char ch = 0;
	while ((ch = getchar()) != EOF)   //保证一直接收键盘字符
	{
		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');
		else{ putchar(ch); }   //向终端输出字符
	}
}

今日还对递归调用有了一个更加深刻的认识然后在深刻了解的基础上练习了两个问题,和大家共勉
1.A,B,C,D,E五个人合伙捕鱼,第二日,A第一个醒来,他将鱼平分为五份,把多余的一条扔回河中,然后自己拿着一份回家,B,C,D,E依次醒来也这样做,问这五个人至少合伙捕到多少条鱼

#include"stdio.h"
#include"stdlib.h"
int fish(int n, int x){ 
	if (x % 5 == 1){//定义总鱼数和人数  每次多余一条
		if (n == 1)
			return 1;
		else
			return fish(n - 1, (4 * (x - 1)) / 5);      // 递归调用
	}
	return 0;
}
main(){
	int i = 0;
	int flag = 0;
	int x;
	do
	{
		i++;
		x = 5 * i + 1;      //最小是六
		if (fish(5, x)){     //调用函数
			flag = 1;
			printf("%d\n", x);
		}
	} while (!flag) ;
	system("pause");
}

6.汉诺塔问题

#define _CRT_SECURE_NO_WARNINGS
#include"stdio.h"
#include"stdlib.h"
void hannuo(int n, char A, char B, char C){
	if (n == 1)         //A上剩下了n个移到C
		printf("%d %c %c\n", n, A, C);
	else
	{
		hannuo(n - 1, A, C, B);   //借助C将n-1个盘子从A移到B
		printf("%d %c %c\n", n, A, C);
		hannuo(n - 1, B, A,C);     //借助A将n-1个盘子从B移到C
	}
}
main(){
	int n;
	printf("please input the number of dishes:\n");
	scanf("%d", &n);
	printf("%d\n", n);
	hannuo(n, 'A', 'B', 'C');
	system("pause");
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值