用rand函数产生除数和被除数,让用户输入商和余数,程序给出对错的判断;该函数在stdlib.h库中。要保证多次运行产生不同的随机数
时间: 2023-06-09 07:08:43 浏览: 57
好的,我明白了。下面是C++代码实现:
```c++
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
srand(time(NULL)); // 设置随机数种子,以不同的时间作为参数
// 产生随机的被除数和除数,该范围可以根据需求进行修改
int dividend = rand() % 100 + 1;
int divisor = rand() % 10 + 1;
// 计算正确的商和余数
int quotient = dividend / divisor;
int remainder = dividend % divisor;
// 让用户输入答案
int user_quotient, user_remainder;
cout << "请输入商和余数:" << endl;
cin >> user_quotient >> user_remainder;
// 判断答案是否正确
if(user_quotient == quotient && user_remainder == remainder)
{
cout << "恭喜你,回答正确!" << endl;
}
else
{
cout << "很遗憾,回答错误,正确答案是:" << quotient << " " << remainder << endl;
}
return 0;
}
```
这段代码使用了srand和rand函数来产生随机数,同时根据用户输入的答案来给出相应的判断。每次运行程序都会产生不同的随机数,保证了多次运行产生不同的结果。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)