简体   繁体   English

如何在 C++ 中生成 5 个随机的 3 位数字?

[英]How to generate 5 random 3 digit numbers in C++?

There's this exercise that asks me to generate 5 random 3 digit numbers.有一个练习要求我生成 5 个随机的 3 位数字。 The exercise requests that I generate those random numbers by using digits that are inputted by the user.练习要求我使用用户输入的数字生成这些随机数。 Despite me not knowing how to explain, I could try to give an example:尽管我不知道如何解释,但我可以尝试举一个例子:

std::cout << "a = ";
std::cin >> a;
std::cout << "b = ";
std::cin >> b;
std::cout << "c = ";
std::cin >> c;

I'll input the value of these integers (that have to be smaller than 10).我将输入这些整数的值(必须小于 10)。 Let's say a = 5;假设 a = 5; b = 3; b = 3; c = 7. I need to generate 5 random 3 digit numbers using the values of a,b,c that the user puts in. I am allowed to use a, b and c only once when generating that number. c = 7。我需要使用用户输入的 a、b、c 的值生成 5 个随机的 3 位数字。生成该数字时,我只能使用 a、b 和 c 一次。 Using the example above, this is how the numbers should look like: 537, 375, 357, 753, 735 (just an example, there are more possibilities).使用上面的例子,数字应该是这样的:537、375、357、753、735(只是一个例子,还有更多的可能性)。 No numbers like 557, 533 etc. Can someone help?没有 557、533 等数字。有人可以帮忙吗? Thanks in advance.提前致谢。

Try randomly multiplying the three numbers by 1, 10, and 100. Then add the three.尝试将这三个数字随机乘以 1、10 和 100。然后将三个数字相加。 For example:例如:

5 * 1 = 5
3 * 10 = 30
7 * 100 = 700

5 + 30 + 700 = 735

This will guarantee a valid permutation.这将保证有效的排列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM