明解C语言中级篇第二章练习(剩余部分)

练习2-5

/***********************************************/
//练习2-5
//改写 list2-13
/***********************************************/

#include<stdio.h>
#include"sleep.h"
#include<stdlib.h>
int main()
{
	int stage;//题目数量
	int a, b, c;//三个随机数
	int x;//输入数
	int n;//空格的随机数数目
	clock_t start[10], end[10];//设置两个数组用来存放时钟数

	srand((int)time(NULL));
	printf("扩大视野心算训练开始!!\n");

	for(stage = 0;  stage < 10; stage ++)
	{
		a = 10 + rand() % 90;
		b = 10 +rand() % 90;
		c = 10+ rand() % 90;
		n = rand() % 17;//控制视野范围

		printf("%d%*s+%*s%d%*s+%*s%d:", a , n , " " , n , " " , b , n , " " ,n," ",c);//控制输出格式(距离)

		do
		{
			start[stage] = clock();
			scanf_s("%d",&x);
			if (x == a + b + c)
				break;
			printf("\a回答错误。请重新输入:");
		} while (1);
		end[stage] = clock();
	}
	for (int i = 0; i < stage-1; i++)//输出所用时间
	{
		printf("第%04d题用了%5.1f秒\n", i + 1, (double)(end[i] - start[i]) / CLOCKS_PER_SEC);//一定要注意格式转换(double)
	}
	printf("总共用了%3.1f秒\n",(double)(end[stage-1]-start[0])/CLOCKS_PER_SEC);
}

练习2-6

/***********************************************/
//练习2-6
//使用随机数来决定运算种类,其余同练习2-5
/***********************************************/

#include<stdio.h>
#include"sleep.h"
#include<stdlib.h>
int main()
{
	int stage;	//题目数量
	int a, b, c;//三个随机数
	int x;//输入数
	int n;//空格的随机数数目
	int s;
	clock_t start[10], end[10];//设置两个数组用来存放时钟数

	srand((int)time(NULL));
	printf("扩大视野心算训练开始!!\n");
	for (stage = 0; stage < 10; stage++)
	{
			s = 1 + rand() % 4;//随机选取组合
			a = 1 + rand() % 9;
			b = 1 + rand() % 9;
			c = 1 + rand() % 9;
			n = rand() % 17;//控制视野范围
			switch (s)
			{
			case 1:printf("%d%*s+%*s%d%*s+%*s%d:", a, n, " ", n, " ", b, n, " ", n, " ", c); break;	//控制输出格式(距离)组合1
			case 2:printf("%d%*s+%*s%d%*s-%*s%d:", a, n, " ", n, " ", b, n, " ", n, " ", c); break;	//组合2
			case 3:printf("%d%*s-%*s%d%*s+%*s%d:", a, n, " ", n, " ", b, n, " ", n, " ", c); break;	//组合3
			case 4:printf("%d%*s-%*s%d%*s-%*s%d:", a, n, " ", n, " ", b, n, " ", n, " ", c); break;		//组合4
			}
			do
			{
				start[stage] = clock();
				
				scanf_s("%d", &x);
				if (s==1)
				{
					if (x == a + b + c)
						break;
					printf("\a回答错误。请重新输入:"); 
				}
				else if (s==2)
				{
					if (x == a + b - c)
						break;
					printf("\a回答错误。请重新输入:");
				}
				else if (s == 3)
				{
					if (x == a - b + c)
						break;
					 printf("\a回答错误。请重新输入:");
				}
				else
				{
					if (x == a - b - c)
						break;
					printf("\a回答错误。请重新输入:");
				}
			} while (1);
			end[stage] = clock();
		}
		for (int i = 0; i < stage ; i++)//输出所用时间
		{
			printf("第%04d题用了%5.1f秒\n", i + 1, (double)(end[i] - start[i]) / CLOCKS_PER_SEC);//一定要注意格式转换(double)
		}
		printf("总共用了%3.1f秒\n", (double)(end[stage - 1] - start[0]) / CLOCKS_PER_SEC);
	}
	



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值