簡體   English   中英

計算元素頻率的 C 程序

[英]C Program to Count Frequency of Element

我有這個代碼,它從用戶輸入的數字中找到數字的出現。 我的問題是如何首先由用戶輸入數字,然后讓用戶在序列中找到他的號碼。 如果這個問題很愚蠢,我很抱歉,但我只是一個試圖自學編程的初學者:) 非常感謝您的幫助!

這是我當前的代碼:

int main()
{
    int num, remainder, k, count = 0;

    printf("Which number do you want to find:  \n");
    scanf("%d", &k);

    while(1)
    {
        printf("Enter numbers(end by pressing 0): ");
        scanf("%d", &num);

        remainder=num;
        if(remainder==k)count++;
        if(remainder==0) break;

    }


    printf("\Number %d occurs %d times.\n", k, count);


    getch();
    return 0;
}

這是我嘗試過的:

int main()
{
    int num, remainder, k, count = 0;


    while(1)
    {
        printf("Enter numbers(end by pressing 0): ");
        scanf("%d", &num);

        remainder=num;
        if(remainder==k)count++;
        if(remainder==0) break;

    }

    printf("Which number do you want to find:  \n");
    scanf("%d", &k);

    printf("\Number %d occurs %d times.\n", k, count);


    getch();
    return 0;

如果您想先讀取數字列表,則需要使用變量以某種方式將其存儲在內存中,然后才能在該列表中找到數字。

這可以通過使用 C 中的arrays概念來完成。

通常,數組存儲類似類型數據的明確集合/列表。 例如,一個int數組存儲一個整數集合。

你可以在這里閱讀更多關於它們的信息,甚至你也可以用谷歌搜索。

來到您的代碼,您可以像這樣在數組中聲明和存儲數據,

int numbers[10]; // stores 10 integers, size 10 is static here.
int remainder, k, count = 0;

printf("Enter 10 digits to store : ");
for(int i = 0; i < 10; i++) { // this is a for-loop that executes below block 10 times
    scanf("%d", &numbers[i]); // read a number to location i
}


printf("Which number do you want to find:  \n");
scanf("%d", &k);

// now use for-loop to iterate over the numbers array(while-loop can also be used)
for(int i = 0; i < 10; i++) {
    remainder=numbers[i];
    if(remainder==k)count++; // you can also directly compare numbers[i] to k
    // below line is not needed since the loop stops at 10th iteration
    // if(remainder==0) break;
}

printf("\Number %d occurs %d times.\n", k, count);

筆記:

上面的代碼僅在用戶想要給出 10 個數字時才有效。 如果用戶需要提供可變數量的數字以從中找到k ,那么您必須從用戶讀取數組的大小並創建該大小的動態數組。 參考這個

您可以放心地忽略除我之外的所有答案。:)

對於您的任務,您需要稱為單鏈表的數據結構。 理論上它沒有限制,您可以輸入任何數字序列,直到輸入 0。

這是一個演示程序。

#include <stdio.h>
#include <stdlib.h>

struct node
{
    int number;
    struct node *next;
};

struct sequence
{
    struct node *head;
    struct node *tail;
};

int append( struct sequence *seq, int number )
{
    struct node *new_node = malloc( sizeof( *new_node ) );
    int success = new_node != NULL;

    if( success )
    {
        new_node->number =number;
        new_node->next = NULL;
        
        if ( seq->head == NULL )
        {
            seq->head = new_node;
        }
        else
        {
            seq->tail->next = new_node;
        }

        seq->tail = new_node;
    }

    return success;
}

size_t count( const struct sequence *seq, int number )
{
    size_t cnt = 0;

    for ( const struct node *current = seq->head; current != NULL; current = current->next )
    {
        if ( current->number == number ) ++cnt;
    }

    return cnt;
}

void clear( struct sequence *seq )
{
    while ( seq->head != NULL )
    {
        struct node *current = seq->head;
        seq->head = seq->head->next;
        free( current );
    }

    seq->tail = NULL;
}
        
int main(void) 
{
    struct sequence seq = { .head = NULL, .tail = NULL };

    printf( "Enter numbers (end by entering 0): " );

    int number;

    while ( scanf( "%d", &number ) == 1 && number != 0 && append( &seq, number ) );

    number = 0;

    printf( "Which number do you want to find: " );

    scanf( "%d", &number );

    printf( "\nNumber %d occurs %zu times.\n", number, count( &seq, number ) );

    clear( &seq );

    return 0;
}

它的輸出可能看起來像

Enter numbers (end by entering 0): 1 2 3 4 5 6 7 8 9 1 8 7 6 5 4 3 2 1 0
Which number do you want to find: 1
Number 1 occurs 3 times.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM