简单写法:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char a[1005];
int main()
{
int num, i, len;
while(gets(a))
{
len = strlen(a);
num = 0;
for(i = 0; i < len; i++)
{
if(a[i] != ',' && a[i + 1] == ',' && a[i + 2] == ',')num++;
}
printf("%d\n", num);
}
return 0;
}
标准写法:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char a[105];
int p, len;
struct node
{
char data;
struct node *l, *r;
};
struct node *qu[10005];
int front = 0, tail = 0;
struct node *build()
{
p++;
if(p >= len)return NULL;
struct node *h;
if(a[p] == ','){h = NULL;}
else
{
h = (struct node *)malloc(sizeof(struct node));
h -> l = h -> r = NULL;
h -> data = a[p];
h -> l = build();
h -> r = build();
}
return h;
};
int main()
{
while(scanf("%s", a) != EOF)
{
getchar();
p = -1;
len = strlen(a);
struct node *h = build();
front = tail = 0;
qu[tail++] = h;
int num = 0;
while(front < tail)
{
struct node *q = qu[front++];
if(q != NULL)
{
if(q -> l == NULL && q -> r == NULL)num++;
if(q -> l != NULL)qu[tail++] = q -> l;
if(q -> r != NULL)qu[tail++] = q -> r;
}
}
printf("%d\n", num);
}
return 0;
}