随机生成的输出中有多少位数字 1、2、3

  • 本文关键字:数字 多少 输出 随机 c++
  • 更新时间 :
  • 英文 :


正在尝试我在网上看到的一个问题,这些问题要求用户输入随机数生成的时间,并计算生成的数字中有多少位数字 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 部分的代码,它要求用户输入数字并根据用户输入生成随机数

要计算单个整数值中 1、2 和 3 的出现次数,最简单的 IMO 是将整数转换为字符串,然后计算您感兴趣的数字:

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 3n";

结果:此数字:"1243"包含 3 次数字 1、2 或 3

我把它划分为函数,这样更容易理解。 我使用开关大小写是因为这是你问的,但还有其他方法。

#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;
}

相关内容

最新更新