我试图写一个函数,它的阵列的数字由大小和int数组作为输入,并打印各数位的频率。
样本输入和输出:
Input: [1,2,2,3,3,3]
Output:
1 occurs 1 times.
2 occurs 2 times
3 occurs 3 times.
这是我尝试(不是最优雅的):
void freq(int size, int numArray[]) {
int one=0, two=0, thr=0, fou=0, fiv=0, six=0, sev=0, eit=0, nin=0;
int i, j;
for (i = 0; i < size; i++) {
for (j = 1; j < size; j++) {
if (numArray[i] == numArray[j] && numArray[i] == 1) {
one+=1;
}
else if (numArray[i] == numArray[j] && numArray[i] == 2) {
two+=1;
}
else if (numArray[i] == numArray[j] && numArray[i] == 3) {
thr+=1;
}
else if (numArray[i] == numArray[j] && numArray[i] == 4) {
fou+=1;
}
else if (numArray[i] == numArray[j] && numArray[i] == 5) {
fiv+=1;
}
else if (numArray[i] == numArray[j] && numArray[i] == 6) {
six+=1;
}
else if (numArray[i] == numArray[j] && numArray[i] == 7) {
sev+=1;
}
else if (numArray[i] == numArray[j] && numArray[i] == 8) {
eit+=1;
}
else if (numArray[i] == numArray[j] && numArray[i] == 9) {
nin+=1;
}
}
}
printf("1 occurs %d times.\n", one);
printf("2 occurs %d times.\n", two);
printf("3 occurs %d times.\n", thr);
printf("4 occurs %d times.\n", fou);
printf("5 occurs %d times.\n", fiv);
printf("6 occurs %d times.\n", six);
printf("7 occurs %d times.\n", sev);
printf("8 occurs %d times.\n", eit);
printf("9 occurs %d times.\n", nin);
}
这有问题。 如果我用上面同样的例子,这是我得到:
Input: [1,2,2,3,3,3]
Output:
1 occurs 0 times.
2 occurs 4 times.
3 occurs 9 times.
Answer 1:
嵌套循环的方法是没有意义的,你只需要看看每个数字一次算吧。 当然,数组更有意义,以保持计数器:
void freq(int size, const int *numbers)
{
unsigned int counts[10] = { 0 };
for(int i = 0; i < size; ++i)
{
const int here = numbers[i];
if(here >= 1 && here <= 9)
counts[here]++;
}
for(int i = 1; i < 10; ++i)
printf("%d occurs %u times\n", i, counts[i]);
}
Answer 2:
你的逻辑是有缺陷的。 你想要的是在阵列上迭代一次 ,并检查每个元素。 是这样的:
for(int i = 0; i < size; i++){
if (numArray[i] == NUMBER)
NUMBER_COUNTER ++;
.
.
.
或者,使用你原来的代码:
void freq(int size, int numArray[]) {
int one=0, two=0, thr=0;
int i;
for (i = 0; i < size; i++) {
if (numArray[i] == 1) {
one+=1;
}
else if (numArray[i] == 2) {
two+=1;
}
else if (numArray[i] == 3) {
thr+=1;
}
}
printf("1 occurs %d times.\n", one);
printf("2 occurs %d times.\n", two);
printf("3 occurs %d times.\n", thr);
}
我认为是更优雅的另一种解决方案是这样的:创建9个元素(假设你要计算的9位...频率)的新阵列,并增加了发现数字的槽......是这样的:
void freq(int size, int numArray[]) {
int freq_arr[size];
int i;
for(i = 0; i < size; i++)
freq_arr[ numArray[i] ] ++;
for(i = 0; i < size; i++)
printf("i: %d = %d\n", i, freq_arr[i]);
Answer 3:
你有什么需要内部循环? 你需要的是在每个数字看起来曾经在每次迭代,从而指望它。 它比你想象的更简单。 更改您的代码:
void freq(int size, int numArray[]) {
int one=0, two=0, thr=0, fou=0, fiv=0, six=0, sev=0, eit=0, nin=0;
int i, j;
for (i = 0; i < size; i++) {
if (numArray[i] == 1)
one+=1;
else if (numArray[i] == 2)
two+=1;
else if (numArray[i] == 3)
thr+=1;
else if (numArray[i] == 4)
fou+=1;
else if (numArray[i] == 5)
fiv+=1;
else if (numArray[i] == 6)
six+=1;
else if (numArray[i] == 7) {
sev+=1;
else if (numArray[i] == 8)
eit+=1;
else if (numArray[i] == 9)
nin+=1;
}
printf("1 occurs %d times.\n", one);
printf("2 occurs %d times.\n", two);
printf("3 occurs %d times.\n", thr);
printf("4 occurs %d times.\n", fou);
printf("5 occurs %d times.\n", fiv);
printf("6 occurs %d times.\n", six);
printf("7 occurs %d times.\n", sev);
printf("8 occurs %d times.\n", eit);
printf("9 occurs %d times.\n", nin);
}
另一种方法,而不是保持10个int
变量作为计数器将保持十个数组int
S,就像这样:
void freq(int size, int numArray[]) {
int i;
int counters[10];
//initialize the array's elements to zero
for (i = 0; i < 10; i++)
counters[i] = 0;
for(i = 0; i < size; i++)
{
if (numArray[i] = i)
counters[i]++;
}
}
Answer 4:
void freq(int size, const int numArray[]) {
int cnt[10] = {0};
for (int i=0; i<size; i++) {
if (0 <= numArray[i] && numArray[i] <= 9)
cnt[numArray[i]]++;
}
// cnt = {0, 3, 1, 0, 0, 0, 0, 0, 0, 0}
}
// test
int main(int argc, char* argv[])
{
int arr[4] = {1,1,1,2};
freq(4, arr);
return 0;
}
文章来源: Find frequency of digits in an int array in C