2017-08-17 25 views
-1

正在嘗試一個我在網上看到的問題,這些問題需要用戶輸入隨機數的生成次數和計數有多少位數字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部件,其要求用戶輸入數目的代碼以及基於用戶輸入產生隨機數

+0

'std :: isdigit'和'std :: map'。 – LogicStuff

+1

@moon請顯示你到目前爲止的代碼。這可能是你得到低價的主要原因,因爲你很好地描述了這個問題。 – BoBTFish

+1

@BoBTFish我用代碼編輯了這個問題。 – moon

回答

1

要計算的1,2和3發生的次數在一個整數值將是最簡單的海事組織整數轉換成字符串,然後算你感興趣的數字:

int countDigits(int number, std::string digitsOfInterest = "123") { 
    int ret = 0; 
    std::string numberAsString = std::to_string(number); // convert it to string 
    for (const char& digit : numberAsString) { // loop over every character 
    if (digitsOfInterest.find(digit) != std::string::npos) { 
     ret++; 
    } 
    } 
    return ret; 
} 

只是一個隨機生成的數字傳遞到功能,並添加了結果。如您所見,通過將digitsOfInterest更改爲另一個字符串,可以更改要計數的數字。

PS .:因爲我假設你有權訪問C++ 11,所以我建議將你的號碼更改爲<random>


下面是一個非C++ 11解決方案,它的工作方式相同的上述一個作用:

int countDigits(int number, std::string digitsOfInterest = "123") { 
    int ret = 0; 
    std::ostringstream oss; 
    oss << number; 
    std::string numberAsString = oss.str(); // convert it to string 
    for (size_t i = 0; i < numberAsString.size(); ++i) { // loop over every character 
    if (digitsOfInterest.find(numberAsString[i]) != std::string::npos) { 
     ret++; 
    } 
    } 
    return ret; 
} 

下面是一個例子:

std::cout << "This number: '1243' contains " << countDigits(1243) << " times a digit of 1,2 or 3\n"; 

結果:該數: '1243'包含1,2或3個數字的3倍

+0

在這種情況下,你把「123」,意味着它計數123或計爲「1」,「2」,「3」? – moon

+0

它對字符串中的每個字符進行計數,所以'1','2','3'。 – muXXmit2X

+0

注意到謝謝 – moon

-1
#include<bits/stdc++.h> 
using namespace std; 
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{ 
     int count = 0; 
     while(i++ < input) { 
      ranNum= (rand() % 10000); 
      cout<<ranNum<<"\n"; 
      if(ranNum%10 == 1 || ranNum%10 == 2 || ranNum%10 == 3) 
      { 
       count++; 
      } 
      ranNum = ranNum/10; 

      if(ranNum%10 == 1 || ranNum%10 == 2 || ranNum%10 == 3) 
      { 
       count++; 
      } 
      ranNum = ranNum/10; 

      if(ranNum%10 == 1 || ranNum%10 == 2 || ranNum%10 == 3) 
      { 
       count++; 
      } 
      ranNum = ranNum/10; 
      if(ranNum%10 == 1 || ranNum%10 == 2 || ranNum%10 == 3) 
      { 
       count++; 
      } 

     } 
     cout <<count; 
    } 
    return 0; 
} 
+0

爲什麼會有這麼多類似的if?介意解釋? – moon

+0

你也可以在循環內寫入。重複是數字中出現的數字的數量。 –

+0

如果聲明不起作用? – moon

0

我把它分成了幾個函數,這樣會更容易理解。 我用開關盒,因爲你問,但也有其他方式。

#include<time.h> 
#include <iostream> 
#include <stdlib.h> 

// This function is the UI, i.e. asking the user how many numbers to generate 
int HowManyNumbers() 
{ 
    int input; 
    std::cout << "Enter the number of time to loop" << std::endl; 
    std::cin >> input; 
    return input; 
} 

// This function counts 1,2,3 for individual number 
int Count123InNum(int num) 
{ 
     int count = 0; 
     while(num) 
     { 
      int lastDig = num % 10; 
      // count only if lastDigit in number is 1,2 or 3 
      switch(lastDig) 
      { 
       case 1: 
       case 2: 
       case 3: 
        ++count; 
        break; 

       default: 
        break;       
      } 
      num /= 10; 
     } 
     return count; 
} 

// This function receives number of random numbers to generate, 
// and its output is a print of the numbers and the joint occurences of 1,2 and 3 
void Get123FromRandomNums(int nRandNumbers) 
{ 
    srand(time(NULL)); 
    std::cout << "In the numbers: "; 
    int count = 0; 

    while(nRandNumbers--) 
    { 
     int num = rand() % 10000; 
     std::cout << num << " "; 
     count += Count123InNum(num); 
    } 
    std::cout << "There are " << count << " digits 1, digit 2, digit 3." << std::endl; 
} 

int main() 
{ 
    // Get number of random numbers (i.e. iterations) 
    int nRandNumbers = HowManyNumbers(); 

    // check validity 
    if (nRandNumbers < 0 || nRandNumbers > 50) 
    { 
     std::cout << "Invalid entry" << std::endl; 
    } 
    else 
    { 
     //if valid, count and print 1,2,3 occurences 
     Get123FromRandomNums(nRandNumbers); 
    } 
    return 0; 
}