gpt4 book ai didi

计算使用特定值调用函数的次数

转载 作者:行者123 更新时间:2023-11-30 20:39:37 25 4
gpt4 key购买 nike

我想用数组中的键创建一个表。有没有简单的方法可以做到这一点。

int array1[] = {1,5,3,8,9,11};
// table[1]
// table[5]
// table[3]

int count(int a)
{

//a is one of the values in array. array1[] = {1,5,3,8,9,11};
// for ex 3.
// I have to figure out how many times this function was called with what values 1/5/3/8/9/11
table[3]++;
}

最佳答案

简单的代码

  #include<stdio.h>
int n = 6; //number of elements in array1
int array1[] = {1,3,5,8,9,11};
int *funCount;//Count of elements in array1
int count(int a)
{
int i;
for(i = 0; i < n; i++)
if(a == array1[i])
break;
funCount[i]++;
}

int main()
{
funCount = (int*)calloc(n, sizeof(int));
int i;
count(1);
count(3);
count(5);
count(8);
count(9);
count(11);
for(i = 0; i < n; i++)
printf("%d ",funCount[i]);
return 0;
}

如果您的 array1 很小,那么这种方法是可以的!否则我建议您使用散列

关于计算使用特定值调用函数的次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26411421/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com