简体   繁体   中英

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

There's this exercise that asks me to generate 5 random 3 digit numbers. 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). Let's say a = 5; 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. Using the example above, this is how the numbers should look like: 537, 375, 357, 753, 735 (just an example, there are more possibilities). No numbers like 557, 533 etc. Can someone help? Thanks in advance.

Try randomly multiplying the three numbers by 1, 10, and 100. Then add the three. For example:

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

5 + 30 + 700 = 735

This will guarantee a valid permutation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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