1.利用随机数生成一定范围内的数字
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int x,y,z,i;
srand((unsigned)time(NULL));
for(i=1;i<=100;i++)
{
x= rand()%5+1;//1-5
//1.定义变量x,值为一个1-5之间的随机数
//结题思路:由rand函数概念可知 生成m-n的随机数 题目说明生成1-5 则m=1 n=5 根据规则可知rand()%(n-m+1)+m 则需写成(5-1+1)+1 即5+1
//2.定义变量y,值为一个 6-10之间的随机数
y= rand()%5+6;//6-10
//3.定义变量z,值为一个 20-30之间的随机数
z=rand()%9+20;
printf("%d:x:%d\ty:%d\tz:%d\n",i,x,y,z);
}
}
2.定义变量x、y和z。随机生成两个数字x和y的值,其值为1-10之间的数字。随机生成一个数字z,其值为1-5之间的数字。
//按照如下格式输出:xzy。假设x值为8 y的值为9 z的值为2 则输出: 829
int x,y,z;
srand((unsigned)time(NULL));
x=rand()%10+1;
y=rand()%10+1;
z=rand()%5+1;
printf("%d%d%d",x,z,y);
//3.定义变量x,值为1-5之间的随机数。使用if-else对x的值进行判断,
//如果x随机生成的数为1,则输出符号+,如果x为2,则输出减号-;如果x为3,则输出乘号*;如果x为4,则输出除号/,如果x为5,则输出取余号%。
int x;
srand((unsigned)time(NULL));
x=rand()%5+1;
if(x==1)
printf("+");
else if(x==2)
printf("-");
else if(x==3)
printf("*");
else if(x==4)
printf("/");
else if(x==5)
printf("%");
//4.定义变量x、y和z。随机生成两个数字x和y的值,其值为1-10之间的数字。随机生成一个数字z,其值为1-5之间的数字。
// 根据不同的值判断不同的+-*/% 将结果按照 1*2 2-4类似的形式输出
//tips: printf("%d%c%d\n",x,'/',y);
int x,y,z;
srand((unsigned)time(NULL));
x=rand()%10+1;
y=rand()%10+1;
z=rand()%5+1;
printf("%d%d%d\n",x,z,y);
if(z==1)
printf("%d%c%d\n",x,'+',y);
else if(z==2)
printf("%d%c%d\n",x,'-',y);
else if(z==3)
printf("%d%c%d\n",x,'*',y);
else if(z==4)
printf("%d%c%d\n",x,'/',y);
else if(z==5)
printf("%d%c%d\n",x,'%',y);
//5.在第6题的基础上,定义整型变量result和answer,result的值为不同运算的结果, answer的值为从键盘上输入的数字
//在完成题目的输出后,从键盘上输入answer的值,通过if-else语句判断answer的结果是否与result的结果相同。如果相同,则输出回答正确,否则输出回答错误。
int x,y,z,answer,result;
srand((unsigned)time(NULL));
x=rand()%10+1;
y=rand()%10+1;
z=rand()%5+1;
printf("%d%d%d\n",x,z,y);
if(z==1)
{
printf("%d%c%d\n",x,'+',y);
result=x+y;
}
else if(z==2)
{
printf("%d%c%d\n",x,'-',y);
result=x-y;
}
else if(z==3)
{
printf("%d%c%d\n",x,'*',y);
result=x*y;
}
else if(z==4)
{
printf("%d%c%d\n",x,'/',y);
result=x/y;
}
else if(z==5)
{
printf("%d%c%d\n",x,'%',y);
result=x%y;
}
scanf("%d",&answer);
if(answer==result)
printf("right");
else
printf("wrong");
//6.定义变量i的值为1,使用goto或者while将第5题的运算循环10次。
int x,y,z,answer,result,i=1;
srand((unsigned)time(NULL));
while(i<=10)
{
x=rand()%10+1;
y=rand()%10+1;
z=rand()%5+1;
printf("第%d题:\n",i);
if(z==1)
{
printf("%d%c%d=?\n",x,'+',y);
result=x+y;
}
else if(z==2)
{
printf("%d%c%d=?\n",x,'-',y);
result=x-y;
}
else if(z==3)
{
printf("%d%c%d=?\n",x,'*',y);
result=x*y;
}
else if(z==4)
{
printf("%d%c%d=?\n",x,'/',y);
result=x/y;
}
else if(z==5)
{
printf("%d%c%d=?\n",x,'%',y);
result=x%y;
}
scanf("%d",&answer);
if(answer==result)
printf("right\n");
else
printf("wrong\n");
i++;
}
最终代码:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
srand((unsigned)time(NULL));
int x,y,z,result,answer,i=1,score=0;//i用于控制循环次数 //score用于统计最后得分
while(i<=10)
{
printf("第%d题:",i);
x=rand()%10+1;
y=rand()%10+1;
z=rand()%5+1;
if(z==1)
{
printf("%d%c%d=?\n",x,'+',y);
result=x+y;
}
else if(z==2)
{
printf("%d%c%d=?\n",x,'-',y);
result=x-y;
}
else if(z==3)
{
printf("%d%c%d=?\n",x,'*',y);
result=x*y;
}
else if(z==4)
{
printf("%d%c%d=?\n",x,'/',y);
result=x/y;
}
else if(z==5)
{
printf("%d%c%d=?\n",x,'%',y);
result=x%y;
}
scanf("%d",&answer);
if(result==answer)
{
printf("回答正确\n");
score=score+10;
}
else
printf("回答错误\n");
i++;
}
printf("最终得分%d",score);
}