正在嘗試一個我在網上看到的問題,這些問題需要用戶輸入隨機數的生成次數和計數有多少位數字1,數字2,數字3,是否有他生成的數字。隨機生成的輸出中有多少個數字1,2,3?
For example
Enter number of time to loop : 4
2241 1204 5532 8593
There are 8 digits 1, digit 2 and digit 3.
代碼:
int main()
{
int input;
int ranNum;
cout << "Enter the number of time to loop" << endl;
cin >> input;
srand(time(NULL));
int i = 0;
if (input < 0 || input > 50)
{
cout << "Invalid entry";
}
else
{
while(i++ < userInput)
{
ranNum = (rand() % 10000);
cout << ranNum<< " ";
}
}
return 0;
}
的問題指出,使用開關的情況下會更容易把它完成。但是,我不太確定交換機箱如何爲此工作。或者,有沒有其他方法可以使用?
我已經完成進行第2部件,其要求用戶輸入數目的代碼以及基於用戶輸入產生隨機數
'std :: isdigit'和'std :: map'。 – LogicStuff
@moon請顯示你到目前爲止的代碼。這可能是你得到低價的主要原因,因爲你很好地描述了這個問題。 – BoBTFish
@BoBTFish我用代碼編輯了這個問題。 – moon