5

这是我想要实现的输出:

(随机 var1)/(随机 var2)=
Ans:(var ans)

我已经做过加法、减法和乘法,但我遇到了除法的困难,因为我需要精确的被除数和除数来除,这样就不难回答了。

例子:

40/5=
答案:8

不是这个:

7/5=
答案:浮点值

这是我的代码:

int x,num,num2,ans,quo,score=0;
time_t t;

clrscr();

for(x=0;x<5;x++)
{
    srand((unsigned) time(&t));
    num2=rand()%10;
    quo=num/num2;

    if(num2==1)
    {
        num=rand()%9;
    }
    else if(num2==2)
    {
        num=2 + (2 * rand ()) %18; //this should return a value divisible by 2(ranges 0-18)
    }
    else if(num2==3)
    {
        num=rand()% //this should return a value divisible by 3 only (ranges 0-27)
    }
    else if(num2==4)
    {
        num=rand()% //this should return a value divisible by 4 only (ranges 0-36)
    }
    else if(num2==5)
    {
        num=rand()% //this should return a value divisible by 5 only (ranges 0-45)
    }
    else if(num2==6)
    {
        num=rand()% //this should return a value divisible by 6 only (ranges 0-54)
    }
    else if(num2==7)
    {
        num=rand()% //this should return a value divisible by 7 only (ranges 0-63)
    }
    else if(num2==8)
    {
        num=rand()% //this should return a value divisible by 8 only (ranges 0-72)
    }
    else if(num2==9)
    {
        num=rand()% //this should return a value divisible by 9 only (ranges 0-81)
    }
    else if(num2==10)
    {
        num=rand()% //this should return a value divisible by 10 only (ranges 0-90)
    }
    else
    {
    }

    gotoxy(30,14);
    printf("\n%d / %d = ",num,num2);
    printf("\nAns: ");
    scanf("%d",&ans);
} 
4

5 回答 5

3

您可以选择一个随机结果并创建问题

denominator = 14 (randomly chosen)
result = 21 (randomly chosen)

numerator = denominator * result

然后你问多少钱numerator / denominator

于 2013-08-20T14:26:12.993 回答
1

简单地:

num2=rand()%9+1;
quo=rand()%10;
num = quo * num2;
printf("\n%d / %d = ",num,num2);

此外,您应该将srand()调用移至循环之前。否则,如果有人回答问题太快,他们会再次收到相同的问题。

于 2013-08-20T14:27:48.383 回答
0

使用模数运算符进行测试:

int test, result;

test = 6%3;  //test == 0
if(test == 0) result = 6/3; //test passes, assignment made

test = 7%3; //test == 1
if(test == 0) result = 7/3; //test fails, assignment not made

这将保证产生整数值的比率。

此外,随机生成器函数可能会使事情变得更容易,如下所示:

int randGenerator(int min, int max)
{
    int random, trying;

    trying = 1;         
    while(trying)
    {
        srand(clock());
        random = (rand()/32767.0)*(max+1);
        (random >= min) ? (trying = 0) : (trying = 1);
    }
    return random;
}
于 2013-08-20T14:18:24.940 回答
0

答案:

int x,num,num2,ans,quo,score=0; time_t t;

clrscr(); srand((无符号)时间(&t));

for(x=0;x<5;x++)
{
num2=rand()%9;

if(num2==1)
{
 srand((unsigned) time(&t));
 num=rand()%9;
}
else if(num2==2)
{
 srand((unsigned) time(&t));
 num=(rand()%9+1)*2;
}
else if(num2==3)
 {
  srand((unsigned) time(&t));
  num=(rand ()%9+1)*3;
 }
else if(num2==4)
 {
  srand((unsigned) time(&t));
  num=(rand()%9+1)*4;
 }
else if(num2==5)
 {
  srand((unsigned) time(&t));
  num=(rand()%9+1)*5;
 }
else if(num2==6)
 {
  srand((unsigned) time(&t));
  num=(rand()%9+1)*6;
 }
else if(num2==7)
 {
  srand((unsigned) time(&t));
  num=(rand()%9+1)*7;
 }
else if(num2==8)
 {
  srand((unsigned) time(&t));
  num=(rand()%9+1)*8;
 }
else if(num2==9)
 {
  srand((unsigned) time(&t));
  num=(rand()%9+1)*9;
 }
else if(num2==10)
 {
  srand((unsigned) time(&t));
  num=(rand()%9+1)*10;
 }
else
 {
  clrscr();
  printf("");
 }

quo=num/num2;
clrscr();
gotoxy(30,14);
printf("\n%d / %d = ",num,num2);
printf("\nAns: ");
scanf("%d",&ans);

}

于 2013-08-21T14:19:19.657 回答
0

你可以做

float quo=(float)num/num2;
printf("%f\n", quo);

并打印确切的结果

如果你想要它们之间可分的随机数,那么你想要别的东西。

num而且您之前没有启动num/num2;,也没有检查 num2 是否为零,因此您可能会被抛出。

最后你可以做类似的事情

num2=rand()%10;
num=rand();
while((float)num/num2 != (float)(num/num2))
    num=rand();
int quo=num/num2;

如果绝对只能接受可分割的夫妻

于 2013-08-20T14:10:25.303 回答