陈都灵照片大全:请问这个问题怎么实现,C语言

来源:百度文库 编辑:神马品牌网 时间:2024/05/05 17:03:08
【问题描述】
输入若干个整数,统计出现次数最多的那个整数。如果出现最多的整数有两个以上,打印最早输入的那个整数。
【输入形式】
从标准输入读取输入。第一行只有一个数字N(1≤N≤10000),代表整数的个数。以后的N行每行有一个整数。
【输出形式】
向标准输出打印出现次数最多的那个数字。
【输入样例】
6
11
0
-1
20
0
300
【输出样例】
0

【样例说明】
输入6个整数,其中出现次数最多的是0,共出现两次。

不可以使用数学库函数

#include<stdio.h>
struct num
{
int n;
int count;

};

struct num nn[100];

main()
{

int *a,*b;
int n,i,t;

while(t=scanf("%d\n",&n),t!=-1)
{
功能是输入数字,如果发现有重复的数字,让n对应的.count的值加1
*a++=t;
}

{

这部分为判断.count里哪个数最大,并输出其对应的n值
}

}

请帮忙,谢谢!
程序正确回报50分

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

typedef struct tag_num
{
int data;
int count;
struct tag_num *next;
}NUM;

/* insert number and count it */
void put_number( NUM *head, int num );

/* get number which have max count */
int get_max_num( NUM *head );

/* init link */
NUM* init_link();

/* destroy link */
void destroy_link( NUM *head );

int main( int argc, char *argv[] )
{
int i, num = 0, n = 0;
NUM *head = 0;

/* init link */
head = init_link();

/* read N */
printf( "Input N: " );
scanf( "%d", &n );

/* read enough number*/
printf( "Now, input %d number:\n", n );
for ( i = 0; i < n; i++ )
{
scanf( "%d", &num );
put_number( head, num ); /* insert and count */
}

num = get_max_num( head ); /* get max count */

printf( "Max Count Number: %d\n", num );

/* destroy link */
destroy_link( head );

return 0;
}

void put_number( NUM *head, int num )
{
NUM *p = NULL;

/* searching 'num' in link. increase count when find it
, or add a new node */
for ( p = head; p->next != NULL; p = p->next )
{
/* just find it */
if ( p->next->data == num )
{
/* increase */
p->next->count++;

/* save max count num */
if ( p->next->count > head->count )
{
head->data = num;
head->count = p->next->count;
}

break; /* out of cycle */
}
}

/* not found */
if ( p->next == NULL )
{
p->next = (NUM*)malloc( sizeof( NUM ) );
p = p->next;
p->data = num;
p->count = 1;
p->next = NULL;

if ( head->count < 1 )
{
head->data = num;
head->count = 1;
}
}
}

int get_max_num( NUM *head )
{
NUM *p = NULL;

/* find first max count number */
for ( p = head->next; p != NULL && p->count != head->count; p = p->next );

if ( p != NULL )
{
return p->data;
}
else
{
/* N <= 0 */
exit( 1 );
}
}

NUM* init_link()
{
NUM *head = NULL;

/* init head node */
head = (NUM*)malloc( sizeof( NUM ) );
head->data = 0;
head->count = 0;
head->next = NULL;

return head;
}

void destroy_link( NUM *head )
{
NUM *p = NULL;

/* destroy all node except head */
for ( p = head->next; p != NULL; p = head->next )
{
head->next = p->next;
free( p );
}

/* destroy head noed */
free( head );
}