使用rand()在除法表中生成数字

时间:2013-08-20 14:05:16

标签: c++

这是我想要实现的输出:

  

(随机化var1)/(随机化var2)=
  答:(var ans)

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

示例:

  

五分之四十=
  答案:8

不是这一个:

  

7/5 =
  ans:浮动值

这是我的代码:

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

5 个答案:

答案 0 :(得分:3)

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

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

numerator = denominator * result

然后你问numerator / denominator

多少钱

答案 1 :(得分:1)

简单地:

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

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

答案 2 :(得分:0)

你可以做到

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

并打印确切的结果

如果您想要在它们之间可分割的随机数字,那么您需要其他东西。

你也不会在num之前发起num/num2;,而你也不会检查num2是否为零,这样你就可能被抛出。

finaaly你可以做点像

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

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

答案 3 :(得分: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;
}

答案 4 :(得分:0)

答案:

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

clrscr();     srand((无符号)时间(&amp; 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);

}