数字C++中的重复数字

数字C++中的重复数字,c++,C++,任务是编写一个函数,该函数接受一个数字并查找该数字中重复次数最多的数字。它应该打印找到的数字和重复的次数 当两个数字重复相同的次数时,我遇到了一个问题 例如,对于给定的数字788995,它应该返回8->2\\9->2 我怎么能把它打印出来 以下是函数: void maxDigitInNumber (long long n) { if (n < MIN || n > MAX) { cout << -1; return;

任务是编写一个函数,该函数接受一个数字并查找该数字中重复次数最多的数字。它应该打印找到的数字和重复的次数

当两个数字重复相同的次数时,我遇到了一个问题

例如,对于给定的数字788995,它应该返回8->2\\9->2 我怎么能把它打印出来

以下是函数:

void maxDigitInNumber (long long n)
{
    if (n < MIN || n > MAX)
    {
        cout << -1;
        return;
    }

    n = abs(n);
    int numOfDigits = (int)log10(n)+1;
    int digits[100];
    int helper[100] = {0};
    int counter = 0;
    int maxSize = 0;
    int number = 0;



    for (int i = 0; i <= numOfDigits; i++)
    {
        digits[i] = n%10;
        n /= 10;
    }

    for(int i = 0; i < numOfDigits; i++)
    {
        if(helper[i] == 0)
        {
            counter = 0;

            for(int j = i; j < numOfDigits; j++)
            {
                if(digits[j] == digits[i])
                {
                    counter++;
                    helper[j] = 1;
                }
                if(counter > maxSize)
                {
                    maxSize = counter;
                    number = digits[i];

                }
            }

        }

    }

    if (number == 0)
    {
        for (int i = 0; i < numOfDigits; i++)
        {
            cout << digits[i] << "->" << maxSize << endl;
        }
    }
    else
    {
        cout << number << "->" << maxSize << endl;
    }
}

按如下方式组织您的程序:

一个函数接受要分析的数字并返回一个值。multiset允许同一密钥有多个条目。所以对于788995这个数字,你会得到一个多集{1:[5,7],2:[8,9]} 另一个函数分析multiset并返回集合中排名最高的键的数字。
按如下方式组织您的程序:

一个函数接受要分析的数字并返回一个值。multiset允许同一密钥有多个条目。所以对于788995这个数字,你会得到一个多集{1:[5,7],2:[8,9]} 另一个函数分析multiset并返回集合中排名最高的键的数字。
在选择最大值之前,您应该存储每个数字的计数。然后,您可以在所有计数中选择最大值,并打印与该最大值匹配的所有条目:

int count[10] = {0};
do {
    count[n%10]++;
    n /= 10;
} while (n != 0);
int maxCount = 0;
for (int i = 0 ; i != 10 ; i++) {
    maxCount = max(maxCount, count[i]);
}
bool first = true;
for (int i = 0 ; i != 10 ; i++) {
    if (count[i] == maxCount) {
        if (!first) {
            cout << " \\\\ ";
        } else {
            first = false;
        }
        cout << i << "->" << maxCount;
    }
}

在选择最大值之前,您应该存储每个数字的计数。然后,您可以在所有计数中选择最大值,并打印与该最大值匹配的所有条目:

int count[10] = {0};
do {
    count[n%10]++;
    n /= 10;
} while (n != 0);
int maxCount = 0;
for (int i = 0 ; i != 10 ; i++) {
    maxCount = max(maxCount, count[i]);
}
bool first = true;
for (int i = 0 ; i != 10 ; i++) {
    if (count[i] == maxCount) {
        if (!first) {
            cout << " \\\\ ";
        } else {
            first = false;
        }
        cout << i << "->" << maxCount;
    }
}

只有10个数字,因此数字中数字的直方图只占10个字

// ....
int hist[10] = {};  // Full tally available for further analysis

int max_count = 0;  // result.
int max_digit = -1;

for (int i = 0; i <= numOfDigits; i++)
{
    int digit = n % 10;
    if (++hist[digit] > max_count)
    {
        max_count = hist[digit]; // could also be ++max_count ;)
        max_digit = digit;
    }
    n /= 10;
}
以下是您可以使用的一些算法:

// prints digits with a certain score:
void print_if_score_is(const int hist[10], int score)
{
    for (int i = 0; i < 10; ++i)
        if (hist[i] == score)
            std::cout << "  digit: " << i << ", score: " << score << "\n";
}

int get_next_best_score(const int hist[10], int score)
{
    int new_max = -1;
    for (int i = 0; i < 10; ++i)
        if (hist[i] > new_max && hist[i] < score)
            new_max = i;
    return new_max;
}
用法:

// ....

std::cout << "Digit most frequently found: \n";
print_if_score_is(hist, max_count);

std:: cout << "next in list: \n";
int next_best = get_next_best_score(hist, max_count);
print_if_score_is(hist, next_best);

//...

只有10个数字,因此数字中数字的直方图只占10个字

// ....
int hist[10] = {};  // Full tally available for further analysis

int max_count = 0;  // result.
int max_digit = -1;

for (int i = 0; i <= numOfDigits; i++)
{
    int digit = n % 10;
    if (++hist[digit] > max_count)
    {
        max_count = hist[digit]; // could also be ++max_count ;)
        max_digit = digit;
    }
    n /= 10;
}
以下是您可以使用的一些算法:

// prints digits with a certain score:
void print_if_score_is(const int hist[10], int score)
{
    for (int i = 0; i < 10; ++i)
        if (hist[i] == score)
            std::cout << "  digit: " << i << ", score: " << score << "\n";
}

int get_next_best_score(const int hist[10], int score)
{
    int new_max = -1;
    for (int i = 0; i < 10; ++i)
        if (hist[i] > new_max && hist[i] < score)
            new_max = i;
    return new_max;
}
用法:

// ....

std::cout << "Digit most frequently found: \n";
print_if_score_is(hist, max_count);

std:: cout << "next in list: \n";
int next_best = get_next_best_score(hist, max_count);
print_if_score_is(hist, next_best);

//...


我可以看到你被指派了什么任务,还有你的代码,但是你似乎忘记了包括这个问题。我对两位数字重复相同次数的情况有一个问题。例如,对于给定的数字788995,它应该返回8->2\\9->2。我如何打印它?通过编写这样打印的代码,到底是什么问题?使用10大小的数组并通过索引增加它们的值。当你说“重复”时,你是指该数字的最长重复长度吗,例如。,112对数字1的重复计数为2,但121对数字1的重复计数仅为1。或者你的意思是不一定是连续的数字,在这种情况下,112和121的数字1重复计数都是2。我可以看到你被分配了什么任务,以及你的代码,但你似乎忘记了包括这个问题。我对两个数字重复相同次数的情况有问题。例如,对于给定的数字788995,它应该返回8->2\\9->2。我如何打印它?通过编写这样打印的代码,到底是什么问题?使用10大小的数组并通过索引增加它们的值。当你说“重复”时,你是指该数字的最长重复长度吗,例如。,112对数字1的重复计数为2,但121对数字1的重复计数仅为1。或者你的意思是不一定是连续数字的数量,在这种情况下,112和121对数字1的重复计数都是2。为什么不使用std::map,键是数字,值是它的发生率?这将是非常简单的,我不允许使用任何东西,除了std::iostream和std::cmathEven数组就可以了!您只有0到9之间的10位数字。将数组初始化为0。取模10的数字,增加数组中的值,该数组的索引是所获得的余数。是的,您可以简单地记住遇到的任何数字的最大序列长度。但是您应该坚持使用std::array而不是原始的C数组。是2017年,不是1987年。但是,她不允许使用它!:-为什么不使用std::map,键是数字,值是它的发生率?这将是非常简单的,我不允许使用任何东西,除了std::iostream和std::cmathEven数组就可以了!您只有0到9之间的10位数字。将数组初始化为0。取模10的数字,增加数组中的值,该数组的索引是所获得的余数。是的,您可以简单地记住遇到的任何数字的最大序列长度。但是您应该坚持使用std::array而不是原始的C数组。是2017年,不是1987年。但是,她不允许使用它!:-