如何找到一个整数的最大频率
How do I find the max frequency of a number of an integer?
这是我到目前为止的代码:
#include <iostream>
#define MAX(X,Y) ((X) > (Y) ? (X):(Y))
int frequency(int n);
int main()
{
int x;
std::cout << "Please enter a sequence of numbers."
<< std::endl;
std::cin >> x;
std::cout << "The max frequency of " << x << " is "
<< frequency(x)
<< std::endl;
return 0;
}
int frequency(int n)
{
int A[10] = {0}, rem;
while (n != 0)
{
int rem = (n % 10);
A[rem] += 1;
n = (n / 10);
std::cout << rem << 't' << n
<< std::endl;
}
//MAX(A[rem], n);
}
我如何修改它,使它打印出在用户指定的整数中出现次数最多的数字?
只需跟踪循环中的最大值:
int A[10] = {};
int max = 0;
while (n != 0)
{
int rem = n % 10;
++A[rem];
n /= 10;
if( max == rem or A[max] > A[rem] )
continue;
if( A[rem] > A[max] or rem > max )
max = rem;
}
return max;
请注意,您创建了另一个rem
和数组,您最好删除它,因为您不使用它。
注释2:
没有问题 A[rem] += 1;
n = ( n / 10 );
可以在c++中简单地表示,如我的代码所示。
相关文章:
- 如何反转整数参数包
- enum是C++中的宏变量还是整数变量
- 努力将整数转换为链表。不知道我在这里做错了什么
- 整数不会重复超过随机数
- 在C++中手动调整数组大小
- 检查输入是否不是整数或数字
- C++使用整数的压缩数组初始化对象
- 芬威克树(BIT).找到具有给定累积频率的最小索引,单位为 O(logN)
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 将"打开的CV图像"中的"颜色"转换为整数格式
- 在指针的帮助下,文本文件中单词的频率
- 通过套接字[TCP]传输数据 如何在C / C ++中打包多个整数并使用send() recv()传输数据
- 如何只允许用户输入正整数
- 如何在c++中从文本文件中逐行读取整数
- C++:如何循环通过向量中的整数元素
- 我可以信任表示整数的浮点或双精度来保持精度吗
- 序列化,没有库的整数,得到奇怪的结果
- 如何将字符设置为函数?我对语法感到困惑...设置<char>max_freq(映射<字符,整数>频率)
- 计算整数向量的频率分布
- 如何找到一个整数的最大频率