c语言13之利用时间函数编写一个猜数字大小程序

题目:

利用时间函数编写一个猜数程序

源代码:

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
	/*
	利用时间函数编写一个猜数程序
	*/
	int a, guess = 0;
	int count = 0;
	srand(time(NULL));//产生随机数
	a = rand() % 100 + 1;
	while (a != guess)
	{
		printf("请你输入你猜的数据:\n");
		scanf_s("%d", &guess);
		count++;
		if (guess > a)
		{
			printf("猜大了!\n");
		}
		else if (guess < a)
		{
			printf("猜小了!\n");
		}
		else {
			printf("恭喜你猜对了!\n");
		}
	}
	printf("count = %d\n", count);//总共猜了多少次
	return 0;
}

运行结果图:

c语言13之利用时间函数编写一个猜数字大小程序

知识点:

time,stdlib库函数的使用
srand(time(NULL)); 作用产生0-100的随机数
a = rand() % 100 + 1; 内存要加一

上一篇:猜数字游戏(猜1-100以内的数字)


下一篇:数据结构杂谈之复杂度分析