C++ 数字范围和显示数字重复c++;

C++ 数字范围和显示数字重复c++;,c++,numbers,repeat,C++,Numbers,Repeat,你可以帮我一个不同的计划。。它引入了一系列数字(没有限制,您可以重复数字),以显示输入的每个数字的次数。。 例:1 3 4 3 6 1 结果: 1-2x 3-3x 4-1x 6-1x 谢谢,很好 #include <iostream> #include <cstdlib> using namespace std; int rnd() { int iRand = (rand() % 100) + 1; } int hundred_random() { fo

你可以帮我一个不同的计划。。它引入了一系列数字(没有限制,您可以重复数字),以显示输入的每个数字的次数。。 例:1 3 4 3 6 1 结果: 1-2x 3-3x 4-1x 6-1x

谢谢,很好

#include <iostream>
#include <cstdlib>
using namespace std;
int rnd()
{
    int iRand = (rand() % 100) + 1;
}

int hundred_random()
{
    for (int rCount=0; rCount < 100; ++rCount)
    {
        cout << rnd() << endl;
    }
}

int main()
{
    cout << " The list of Hundred random numbers are- " << endl;
    hundred_random();
    return 0;
}
#包括
#包括
使用名称空间std;
int rnd()
{
int iRand=(rand()%100)+1;
}
int 100_random()
{
对于(int rCount=0;rCount<100;++rCount)
{

cout为了计算每个数字在数字列表中出现的频率,您可以执行以下操作:

#include <iostream>
#include <vector>
#include <map>
#include <cstdlib>
#包括
#包括
#包括
#包括
我们需要将这些头用于输出、存储数字、存储计数,并使用rand()生成示例

std::vector<int> generate_numbers(int amount, int max_num)
{
    std::vector<int> numbers(amount);
    for (int i = 0; i < amount; ++i) {
        numbers[i] = rand() % max_num + 1;
    }
    return numbers;
}
std::向量生成数(整数金额,整数最大值)
{
std::矢量编号(数量);
对于(int i=0;i
生成一组随机数的辅助方法

std::map<int, int> count_numbers(const std::vector<int> &numbers)
{
    // Count numbers
    std::map<int, int> counts; // We will store the count in a map for fast lookup (C++11's unordered_map has even faster lookup)
    for (size_t i = 0; i < numbers.size(); ++i) { // For each number
        counts[numbers[i]]++; // Add 1 to its count
    }
    return counts;
}
std::map count\u编号(const std::vector&numbers)
{
//数数
std::map counts;//我们将把计数存储在一个映射中以便快速查找(C++11的无序映射具有更快的查找速度)
对于(size_t i=0;i
上述方法进行计数,这是您问题的本质。对于我们遇到的每个数字,我们增加其计数

void print_counts(const std::map<int, int> &counts)
{
    for(std::map<int, int>::const_iterator it = counts.begin();
        it != counts.end(); ++it) { // For each number stored in the map (this automatically filters those with count 0)
        std::cout << it->first << ": " << it->second << std::endl; // Output its count
    }
}
无效打印计数(常数标准::映射和计数)
{
对于(std::map::const_迭代器it=counts.begin();
it!=counts.end();++it){//对于存储在映射中的每个数字(这会自动过滤计数为0的数字)

std::首先,您需要什么帮助?到目前为止,您所尝试的内容是否存在特定问题?我们不应该为您编写程序。向我们展示您所尝试的内容。如果您遇到任何问题,我们很乐意为您提供帮助。请看一看。或者,如果您使用C++11。如果您有任何代码示例,请将其编辑到您的问题中,因为您无法格式化code在comments.Btw中。你为什么要在这个程序中使用cstdlib?@PeterNimroot For
rand()
。当然,发布的代码与实际计算给定的数字无关,
rand()
不需要这样做,但是嘿。
int main() {
    srand(0); // So that we get the same result every time
    std::vector<int> numbers = generate_numbers(10000, 500);
    std::map<int, int> counts = count_numbers(numbers);
    return 0;
}