今天上课时老师布置我们计算随机数的四则运算,在二十分鈡的时间里我无法完成,在寒假里玩的太多了,知识都忘得差不多了,在这里我先用time函数,确保随机数生成的不会相同,为了让随机数都是两位数,我用了rand%90+10,使生成的都是两位数,用rand%5随机生成加减乘除,下面是源代码:
#include<iostream>
#include<ctime>
#include <cstdlib>
using namespace std;
void main()
{
int a, b,d, c;
srand(time(NULL));
for (c=1; c <31; c++)
{
a = rand() % 90+10;
b = rand() % 90+10;
d = rand() % 5;
if (d == 1)
cout << "NO." << c << " " << a << " + " << b << " = " << endl;
if (d == 2)
cout << "NO." << c << " " << a << " - " << b << " = " << endl;
if (d == 3)
cout << "NO." << c << " " << a << " * " << b << " = " << endl;
if (d == 4)
cout << "NO." << c << " " << a << " / " << b << " = " << endl;
}