C语言中二维数组里统计不同元素出现的个数

比如二位数组 a,b,d
b,c,d
a,d,e
d,e,f
最后能统计出
a 2次
b 2次
c 1次
d 3次
e 1次
请给出代码,谢谢
我例子中的逗号不用 我只想知道怎么统计出现次数

#include <stdio.h>
#include <conio.h>
#include <malloc.h>

#define I 3
#define K 4

typedef struct mtab
{
char charr;
int times;
struct mtab * next;
}tab;

int main()
{

char arr[I][K]={'a','a','b','t','d','g','b','t','c','c','a','f'};//3X4的字符数组
char* tp=(char*)arr;//用指针处理数组以方便遍历
tab * head=NULL,* p1=NULL,*p2=NULL;
int i;

for (i=(I)*(K);i;i--,tp++)
{
for(p2=p1=head;p1;p1=p1->next) //在链表查找当前指向的字符
{
p2=p1; //p2指向链表最后一个已经存在的表项
if(p1->charr==*tp) //找到了
{
p1->times++; //个数加1
break; //不再继续查找后面的表项
}
}

if(p1)
continue; //链表中已经查到结束本次循环

p1=(tab*)malloc(sizeof(tab)); //链表中还没有当前指向的字符
if(!p1) return 1; //分配内存时失败
p1->charr=*tp; //添加字符
p1->times=1; //初始化为1个因为是新加的
p1->next =NULL;
if(!head) //链表还没建立
head=p1;
else
p2->next=p1;
}

for(p1=head;p1;p1=p1->next) //打印结果
printf("%c %d次\n",p1->charr,p1->times);

getch(); //此语句以便能够看到结果,头文件 conio.h

return 0;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-10-24
#include <iostream>
using namespace std;
int main()
for(int i)
for(int j)
switch(s[][])
case a:
case b:
case c:
初学仅供参考
第2个回答  2010-10-24
逗号也要输入的?
相似回答