给小学生出20以内加减法运算题

img

代码如下:

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main()
{
    int a,b,c,res,i,t;
    int cnt = 0;
    
    srand((unsigned int)time(NULL));
    for (i=0;i<10;i++)
    {
        c = rand()%2; //生成0-1的随机数
        if(c == 0)
        {
            a = rand()%20;//生成0-19的随机数
            b = rand()%(20-a);
            
            printf("%d + %d = ",a,b);
            scanf("%d",&res);
            if(res == a+b)
            {
                cnt++;
                printf("回答正确\n");
            }else
            {
                printf("回答错误\n");
            }
        }else
        {
            a = rand()%20;//生成0-19的随机数
            b = rand()%20; 
            if(a<b)
            {
                t = a;
                a = b;
                b = t;
            }
            printf("%d - %d = ",a,b);
            scanf("%d",&res);
            if(res == a-b)
            {
                cnt++;
                printf("回答正确\n");
            }else
            {
                printf("回答错误\n");
            }
        }
    }
    printf("得分:%d",cnt*10);
    return 0;
}

你题目的解答代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
    int x, y, z, m, k, c, i, f=0;
    srand((unsigned)time(NULL));
    for( i=0; i<10; i++ )
    {
        z = rand() % 21;
        x = rand() % z+1;
        y = z - x;
        m = rand() % 2;
        if (m==0)
        {
            printf("%d + %d = ", x, y);
            k = z;
        }
        else
        {
            printf("%d - %d = ", z, x);
            k = y;
        }
        scanf("%d", &c);
        if(c==k)
        {
            f += 10;
            printf("回答正确\n");
        }
        else
        {
            printf("回答错误\n");
        }
    }
    printf("得分:%d",f);
    return 0;
}

img

如有帮助,望采纳!谢谢!