练习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);
}