나는 하룻밤 동안이 문제를 디버깅했지만 여전히 무엇이 잘못되었는지 전혀 모릅니다. 150과 200이 같은 시간으로 나타나기 때문에 {100,150,150,200,200,250} 인 6 개의 숫자 배열을 입력 한 다음 더 작은 숫자를 출력한다고 가정 해 보겠습니다.
#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)
여기에 내가 미만이어야한다 num-1
당신이 번호를 얻을로, 인덱스는 일반적으로, NUM-0에서 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] 값에를 할당 한 다음 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;
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다