Tag: 频率

查找给定数字组中的数字频率

假设我们在C ++中有一个向量/数组,我们希望计算这N个元素中的哪一个具有最大重复出现次数并输出最高计数。 哪种算法最适合这项工作。 例: int a = { 2, 456, 34, 3456, 2, 435, 2, 456, 2} 输出为4,因为2次出现4次。 这是2次发生的最大次数。

用于大输入的数字频率程序

我编写了以下程序,以找出每个数字在字符数组中出现的次数。 int main(){ char s[2000],count,j=0; fgets(s,2000,stdin); for(int i=0;i<=9;i++) { count=0;j=0; while(*(s+j)) { if(isdigit(*(s+j))) { if(i==(*(s+j)-'0')) count++; } j++; } printf("%d ",count); } return 0; } 但它不适合大量投入。 b3n47b5xf13qlx233rg4u2c949i623e34nt5661se06b675utbpy258wz633855846l761d61x340h1vn19w191sj18v2u333556bh6m5uc4u050am05p961dhmpu6iq4667zg9 预期的产出是 5 10 5 12 8 11 15 4 4 6 但我得到的输出是 5 10 5 12 7 11 13 3 4 5 任何人都可以帮助我找到我出错的地方吗?

C中数组的频率计数是这个代码有效和高效

我在c中有以下代码用于计算数组中的数字频率: #define MAX 10 int flag=0; void display(int no,int cnt,int visi[]);//function declaration int main() { int arr[]={1,1,1,2,3,4,2,2,3,1};//asume any array or we can enter from user int visited[MAX]; int i,j,no,cnt=1; clrscr(); for(i=0;i<10;i++)//loop { no=arr[i]; cnt=1; for(j=i+1;j<10;j++) { if(no==arr[j]) cnt++; } display(no,cnt,visited); } return 0; } void display(int no,int cnt,int visited[]) { int static i; int j; […]

C中每N个元素中最常见的

我有一个大的数组A,大小为[0,8388608]的“相对较小”的整数A [i] = [0,131072],我想找到每个N = 32个元素中最常出现的元素。 什么会更快, A.创建一个大小为131072的关联数组B,迭代32个元素,递增B [A [i]],然后迭代B,找到最大值,将B中的所有元素重置为0,重复| A | / 32次。 B. qsort每32个元素,找到A [i] == A [i-1]的最大范围(因此也是最常见的元素),重复| A | / 32次。 (编辑)C。别的。

在整数数组中查找最大/最小出现次数

我刚刚编写了一个算法,该算法在输入整数数组中查找具有最大/最小出现次数的值。 我的想法是对数组进行排序(所有出现的顺序都是按顺序排列)并使用对来为每个值存储相应的出现次数。 它应该是O(nlogn)复杂度,但我认为有一些常数乘数。 我该怎么做才能提高性能? #include #include #include “e7_8.h” #define N 20 /*Structure for pair*/ typedef struct { int value; int freq; } VAL_FREQ; void get_freq(int *v, int n, int *most_freq, int *less_freq) { int v_i, vf_i, current_value, current_freq; VAL_FREQ* sp = malloc(n*sizeof(VAL_FREQ)); if(sp == NULL) exit(EXIT_FAILURE); mergesort(v,n); vf_i = 0; current_value = v[0]; current_freq = […]

C程序:如何查找字符串中字符的最大/最小频率

我试图找到一种方法来获取字符串中最大/最小字符频率的值 例如,帮助 会有max freq = 2和min freq = 1(有2个e,还有其他字母出现一次) 另一个例如aaaa 会有max freq = 4和min freq = 4 我正在尝试对此进行编程,但在使用scanf接收字符串后我不知道该怎么做。 假设只有小写,并且单词之间没有空格。 此外,我更喜欢powershell搜索优雅的解决方案,包括类型转换和诸如此类的东西。 刚开始使用C,请尽可能简单。 顺便说一句,我不介意它只是一个建议或如何做到这一点的基本程序。 我不一定需要整个代码。 提前致谢

使用RDTSC以C计算CPU频率始终返回0

我们的讲师给出了以下代码,因此我们可以测量一些算法性能: #include #include static unsigned cyc_hi = 0, cyc_lo = 0; static void access_counter(unsigned *hi, unsigned *lo) { asm(“rdtsc; movl %%edx,%0; movl %%eax,%1” : “=r” (*hi), “=r” (*lo) : /* No input */ : “%edx”, “%eax”); } void start_counter() { access_counter(&cyc_hi, &cyc_lo); } double get_counter() { unsigned ncyc_hi, ncyc_lo, hi, lo, borrow; double result; access_counter(&ncyc_hi, […]