常用的标准库函数

    #include <time.h>

    time_t time(time_t *tloc);

    功能:获取当前系统时间,它会返回自1970年1月1日 00:00:00 到现在一共过多少秒(格林时间)。

    int rand(void);

    功能:返回一个随机整数

    void srand(unsigned int seed);

    功能:设置随机数的种子

    int abs(int j);

    功能:求绝对值

    double sqrt(double x);

    功能:对x进行开方

    double pow(double x, double y);

    功能:计算x的y次方

    注意:使用pow、sqrt这种数学函数是,需要额外链接数学库,gcc xxx.c -lm

    int system(const char *command);

    功能:调用系统命令

    练习1:随机产生10个[10,99]的整数,按照升序输出。

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

int main(int argc,const char* argv[])
{
	srand(time(NULL));
	int arr[10];
	for(int i=0,j; i<10; i++)
	{
		int num = rand() % 90 + 10;
		for(j=i; j>0 && num < arr[j-1]; j--)
		{
			arr[j] = arr[j-1];
		}
		arr[j] = num;
	}

	for(int i=0; i<10; i++)
	{
		printf("%d ",arr[i]);
	}
	return 0;
}

    练习2:随机生成一注双色球彩票号,红球一共6组,每组从1-33中抽取一个,六个互相不重复。然后蓝球是从1-16中抽取一个数字,这整个组成的双色球彩票。

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

int main(int argc,const char* argv[])
{
	char red[6] = {} , cnt = 0 , i;

	srand(time(NULL));
	printf("red:");
	while(cnt < 6)
	{
		// 随机生成一个[1,33]之间的整数
		int num = rand() % 33 + 1;

		// 查询num是否重复
		for(i=0; i<cnt; i++)
		{
			// 如果num在数组出现过则循环提前结束
			if(num == red[i])
				break;
		}

		// for循环没有提前结束,num未出现过
		if(i == cnt)
		{
			red[cnt++] = num;
			printf("%d ",num);
		}
	}
	printf("blue:%d\n",rand()%16+1);
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值