[英]Find the number that appears the most in an array
我一直在調試這個問題一晚,但仍然不知道出了什么問題。 假設我輸入了一個由 6 個數字組成的數組,它是 {100,150,150,200,200,250},因為 150 和 200 出現的次數相同,然后輸出較小的數字。
#include <stdio.h>
#include <stdlib.h>
int main() {
int arr[20] = { NULL }, num, result;
printf("Input a number(1-20) and enter a series of numbers in ascending order: \n");
scanf_s("%d", &num);
for (int k = 0; k < num; k++) {
scanf_s("%d", &arr[k]);
}
int c1, c2, i, j;
int temp = 0;
j = result = 0;
c1 = c2 = 1;
for (i = 1; i <= num-2; i++) { /*Add c1 if the value is the same*/
int a = arr[i];
if (arr[i+1] == a) c1++;
else {
j = i + 1;
temp = arr[j];
while (1) {
if (arr[j+1] == temp) {
c2++;
j++;
}
else break;
}
}
if (c2 > c1) { /*Move i to the position after j*/
c1 = 0;
result = temp;
}
if ((c2 < c1) || (c2 == c1)) { /*Move j to the next position*/
result = a;
c2 = 0;
}
i = j + 1;
}
printf("Number that appears the most time:%d\n", result);
return 0;
}
每次我取得一些進展后都會編輯這是我到目前為止所做的。 {100,150,150,200,200,250} 的輸出是正確的,但現在如果我輸入一個包含 8 個數字 {100,100,100,150,150,200,250,300} 的更大數組,循環就會卡住。 幫助@@
錯誤在這部分:
while (i <= num) { /*Add c1 if the value is the same*/
if (arr[i+1] == a)
這里 i 應該小於num-1
,當你得到數字時,索引從 0 到 num-1,通常i<num
,但你已經在循環內完成了i+1
所以循環只能從0
到num-2
。 不過,從你的邏輯來看,它應該是arr[i]
而不是arr[i+1]
而這部分
int a = arr[i];
if (arr[i+1] == a) c1++;
為什么要為 arr[i] 的值分配 a 然后再次檢查 arr[i+1]? 你只會得到 c1 隨着每個重復值的增加。 而這部分在最后。
i=j+1
這導致了無限循環。
這可能會解決您的問題,盡管它不是一個好的解決方案:
#include <stdio.h>
#include <stdlib.h>
int main() {
int arr[20], num, result;
printf("Input a number(1-20) and enter a series of numbers in ascending order: \n");
scanf("%d", &num);
printf("Now enter the numbers");
for (int k = 0; k < num; k++) {
scanf("%d", &arr[k]);
}
int c1, c2, i, j;
int temp = 0;
j = 0;
result=arr[0];
c2 = 1;
for (i = 0; i < num; i++) { /*Add c1 if the value is the same*/
int a = arr[i];
c1=1;
for(j=i+1;j<num;j++)
{
if (arr[i]==arr[j])
{
c1+=1;
}
}
if(c1>c2)
{
c2=c1;
result=arr[i];
}
else if(c1==c2 && result>arr[i])
{
result=arr[i];
}
}
printf("Number that appears the most time:%d\n", result);
return 0;
}
除此之外,它有一個更好的方法來解決你的問題。 創建一個 {int key, int count} 結構,然后使用它來存儲數組中所有唯一成員的計數,然后提取您需要的內容。
這是我所說的代碼,如果您還不了解指針和動態內存,可能很難研究。 但你會明白邏輯。
#include<stdio.h>
#include<stdlib.h>
int main()
{
struct counts
{
int key;
int count;
};
int * data;
int num;
printf("Enter the number of data:");
scanf("%d",&num);
data=malloc(num*sizeof(int));
int i,j;
printf("Enter the data in order:");
for (i=0;i<num;i++)
{
scanf("%d",data+i);
}
struct counts *table;
int table_len=1;
int flag;
table=malloc(sizeof(struct counts));
table[0].key=data[0];
table[0].count=1;
for (i=1;i<num;i++)
{
flag=0;
for(j=0;j<table_len;j++)
{
if (table[j].key==data[i])
{
flag=1;
table[j].count+=1;
break;
}
}
if (flag==0)
{
table=realloc(table,++table_len* sizeof(struct counts));
table[table_len-1].key=data[i];
table[table_len-1].count=1;
}
}
//if you want to see at the table
printf("data\t\tcount\n");
for(i=0;i<table_len;i++)
{
printf(" %d\t\t%d\n",table[i].key,table[i].count);
}
//now to extract the value
int answer,count;
answer=table[0].key;
count=table[0].count;
for(i=1;i<table_len;i++)
{
if(count>table[i].count)
{
continue;
}
else if(count<table[i].count)
{
answer=table[i].key;
count=table[i].count;
}
else if(answer>table[i].key)
{
answer=table[i].key;
}
}
printf("The number with highest frequency is: %d\n",answer);
return 0;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.