| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 524 人关注过本帖
标题:为什么不能输出多组测试用例啊?搞不懂!求助
只看楼主 加入收藏
jj369258
Rank: 4
等 级:业余侠客
帖 子:116
专家分:226
注 册:2010-12-2
结帖率:69.57%
收藏
已结贴  问题点数:10 回复次数:3 
为什么不能输出多组测试用例啊?搞不懂!求助
#include<stdio.h>
#include<stdlib.h>
#define OK 1
#define ERROR 0
#define NULL 0
typedef int Status;
typedef char ElemType;

typedef struct CSNode
{
    ElemType data;
    struct CSNode *lchild,*rchild;
}CSNode,*CSTree;

Status CreateCSTree(CSTree &T)
{
    char d;
    scanf("%c",&d);
    if(d=='#')
        T=NULL;
    else
    {
        T=(CSNode*)malloc(sizeof(CSNode));
        T->data=d;//生成根节点
        CreateCSTree(T->lchild);//构造第一个孩子结点
        CreateCSTree(T->rchild);//构造兄弟结点
    }
    return OK;
}

int LeafCount_CSTree(CSTree T,int &count)
{
    if(T)
    {
        if(T->lchild==NULL&&T->rchild==NULL)
            count++;
        LeafCount_CSTree(T->lchild,count);
        LeafCount_CSTree(T->rchild,count);            
    }
    return count;
}
int main()
{
    int t,i;
    scanf("%d\n",&t);
    for(i=0;i<t;i++)
    {
        CSTree T;
        int count=0;
        CreateCSTree(T);
        LeafCount_CSTree(T,count);
        printf("%d\n",count);
    }
    return 0;
}
搜索更多相关主题的帖子: 测试 include 
2011-11-29 17:51
QQ346957135
Rank: 7Rank: 7Rank: 7
等 级:黑侠
帖 子:148
专家分:658
注 册:2011-8-9
收藏
得分:10 
你这个程序在输入的时候用到scanf函数,在建立二叉树时要清除输入缓冲区的回车符。我举几个例子,以下这个图先进行先序遍历(若左右子树有空,则用#代替)
遍历为:ab#d##ce###
图片附件: 游客没有浏览图片的权限,请 登录注册

叶子节点有两个,输出为2.
运行结果:
图片附件: 游客没有浏览图片的权限,请 登录注册

程序代码:
#include<stdio.h> 
#include<stdlib.h>
#define OK 1
#define ERROR 0
#define NULL 0
typedef int Status;
typedef char ElemType;

typedef struct CSNode
{
    ElemType data;
    struct CSNode *lchild,*rchild;
}CSNode,*CSTree;

Status CreateCSTree(CSTree &T)
{
    char d;
    scanf("%c",&d);
    if(d=='#')
        T=NULL;
    else
    {
        T=(CSNode*)malloc(sizeof(CSNode)); 
        T->data=d;//生成根节点
        CreateCSTree(T->lchild);//构造第一个孩子结点
        CreateCSTree(T->rchild);//构造兄弟结点
    }
    return OK;
}

int LeafCount_CSTree(CSTree T,int &count)
{
    if(T)
    {
        if(T->lchild==NULL&&T->rchild==NULL)
            count++;
        LeafCount_CSTree(T->lchild,count);
        LeafCount_CSTree(T->rchild,count);            
    }
    return count;
}
int main()
{
    int t,i;
    scanf("%d",&t);
    for(i=0;i<t;i++)
    {
        fflush(stdin);//清除缓冲区的内容
        CSTree T;
        int count=0;
        CreateCSTree(T);
        LeafCount_CSTree(T,count);
        printf("%d\n",count);
    }
    return 0;
}


[ 本帖最后由 QQ346957135 于 2011-11-30 13:38 编辑 ]

A real warrior never quits.
2011-11-30 13:22
jj369258
Rank: 4
等 级:业余侠客
帖 子:116
专家分:226
注 册:2010-12-2
收藏
得分:0 
谢谢了!
2011-11-30 16:47
jj369258
Rank: 4
等 级:业余侠客
帖 子:116
专家分:226
注 册:2010-12-2
收藏
得分:0 
回复 2楼 QQ346957135
如果用cin语句呢??呵呵
2011-11-30 16:57
快速回复:为什么不能输出多组测试用例啊?搞不懂!求助
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.025556 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved