计算元素频率的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
概念来完成。
一般来说,数组存储一定的collection/list相似类型的数据。
例如,int
的数组存储整数集合。
您可以阅读更多关于它们的信息 here,甚至您可以 google 它。
使用您的代码,您可以像这样在数组中声明和存储数据,
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
,那么您将必须从用户那里读取数组的大小并创建该大小的动态数组。参考 this
对于您的任务,您需要称为单链表的数据结构。理论上它没有限制,您可以输入任何数字序列,直到输入 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.
我有这段代码可以从用户输入的数字中找到数字的出现。我的问题是如何首先由用户输入数字,然后让用户在序列中找到他的号码。如果这个问题很愚蠢,我很抱歉,但我只是一个试图自学编程的初学者:)非常感谢您的帮助!
这是我当前的代码:
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
概念来完成。
一般来说,数组存储一定的collection/list相似类型的数据。
例如,int
的数组存储整数集合。
您可以阅读更多关于它们的信息 here,甚至您可以 google 它。
使用您的代码,您可以像这样在数组中声明和存储数据,
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
,那么您将必须从用户那里读取数组的大小并创建该大小的动态数组。参考 this
对于您的任务,您需要称为单链表的数据结构。理论上它没有限制,您可以输入任何数字序列,直到输入 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.