| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 945 人关注过本帖
标题:(求助)一道关于树的题目,一直错T T
只看楼主 加入收藏
lanlong2006
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2013-9-25
结帖率:0
收藏
已结贴  问题点数:20 回复次数:3 
(求助)一道关于树的题目,一直错T T
人类学研究对于家族很感兴趣,于是研究人员搜集了一些家族的家谱进行研究。实验中,使用计算机处理家谱。为了实现这个目的,研究人员将家谱转换为文本文件。下面为家谱文本文件的实例:
John
  Robert
    Frank
    Andrew
  Nancy
    David

 家谱文本文件中,每一行包含一个人的名字。第一行中的名字是这个家族最早的祖先。家谱仅包含最早祖先的后代,而他们的丈夫或妻子不出现在家谱中。每个人的子女比父母多缩进2个空格。以上述家谱文本文件为例,John这个家族最早的祖先,他有两个子女Robert和Nancy,Robert有两个子女Frank和Andrew,Nancy只有一个子女David。
 
在实验中,研究人员还收集了家庭文件,并提取了家谱中有关两个人关系的陈述语句。下面为家谱中关系的陈述语句实例:
John is the parent of Robert
Robert is a sibling of Nancy
David is a descendant of Robert

 研究人员需要判断每个陈述语句是真还是假,请编写程序帮助研究人员判断。

输入格式说明:

输入首先给出2个正整数N(2<=N<=100)和M(<=100),其中N为家谱中名字的数量,M为家谱中陈述语句的数量,输入的每行不超过70个字符。
 
名字的字符串由不超过10个英文字母组成。在家谱中的第一行给出的名字前没有缩进空格。家谱中的其他名字至少缩进2个空格,即他们是家谱中最早祖先(第一行给出的名字)的后代,且如果家谱中一个名字前缩进k个空格,则下一行中名字至多缩进k+2个空格。
 
在一个家谱中同样的名字不会出现两次,且家谱中没有出现的名字不会出现在陈述语句中。每句陈述语句格式如下,其中X和Y为家谱中的不同名字:
X is a child of Y
X is the parent of Y
X is a sibling of Y
X is a descendant of Y
X is an ancestor of Y


 输出格式说明:

对于测试用例中的每句陈述语句,在一行中输出True,如果陈述为真,或False,如果陈述为假。

样例输入与输出:
 


序号
 
输入
 
输出
 


1
 
6 5
John
  Robert
    Frank
    Andrew
  Nancy
    David
Robert is a child of John
Robert is an ancestor of Andrew
Robert is a sibling of Nancy
Nancy is the parent of Frank
John is a descendant of Andrew
 
True
True
True
False
False
 


2
 
2 1
abc
  abcdefghij
abcdefghij is a child of abc
 
True
 


3
 
4 5
A
  B
    C
      D
A is a child of D
D is the parent of C
C is a sibling of B
D is a descendant of B
B is an ancestor of C
 
False
False
False
True
True
 
以上是题目,下面贴一下我的代码,求大家帮我看看到底错在哪(卡了2天了T T)



#include<stdio.h>
#include<string.h>
char w[5][20]={{"child"},{"parent"},{"sibling"},{"descendant"},{"ancestor"}};
int kong(char a[]) {
    int n=0,i;
    for(i=0;a[i]!='\0';i++) {
        if(a[i]==' ')
            n++;
        else
            break;
    }
    return n;
}
char fam[200][20];
int num[200]={0};
int search(char a[],int n) {
    int i=1;
    while(1) {
        if(!strcmp(a,fam[i]))
            return i;
        i++;
    }
    return 0;
}
int search1(int n,int x) {
    int i=1;
    while(i<x) {
        if(num[i]==n)
            return i;
        i++;
    }
    return 0;
}
char name[1000],word[1000],n1[100],n2[100],d[100];
int main() {
    int n,m,i,j,a,b,t,x,k,h;
    while(scanf("%d%d",&n,&m)!=EOF) {
        memset(fam,'\0',sizeof(fam));
        memset(num,'\0',sizeof(num));
        scanf("%s",fam[1]);
        getchar();
        a=b=2;
        t=1;
        num[1]=1;
        for(i=2;i<=n;i++) {
            memset(name,'\0',sizeof(name));
            gets(name);
            b=kong(name);
            if(b>a) {
                if(fam[search1(t*2+1,i)][0]!='\0')
                    t=t*2+1;
                else t=t*2;
            }
            else { if(b<a) {
                h=(a-b)/2;
                while(h>=1) {
                    t=t/2;
                    h--;
                }
            }}            
            if(fam[search1(t*2,i)][0]!='\0')
                x=t*2+1;
            else x=t*2;
            num[i]=x;
            for(j=b;name[j]!='\0';j++)
                fam[i][j-b]=name[j];
            a=b;
        }
        for(i=0;i<m;i++) {
            gets(word);
            k=0;h=0;
            memset(n1,'\0',sizeof(n1));
            memset(n2,'\0',sizeof(n2));
            memset(d,'\0',sizeof(d));
            for(j=0;word[j]!='\0';j++) {
                if(word[j]==' ') {
                    k++;
                    h=0;
                    continue;
                }
                if(k==0)
                    n1[j]=word[j];
                if(k==5) {
                    n2[h]=word[j];
                    h++;
                }
                if(k==3) {
                    d[h]=word[j];
                    h++;
                }
            }
            if(!strcmp(d,w[0])) {
                a=search(n1,n);a=num[a];
                b=search(n2,n);b=num[b];
                if(a==b*2||a==(b*2+1))
                    printf("True\n");
                else printf("False\n");
                continue;
            }
            if(!strcmp(d,w[1])) {
                a=search(n1,n);a=num[a];
                b=search(n2,n);b=num[b];
                if(b==a*2||b==(a*2+1))
                    printf("True\n");
                else printf("False\n");
                continue;
            }
            if(!strcmp(d,w[2])) {
                a=search(n1,n);a=num[a];
                b=search(n2,n);b=num[b];
                if((a==b+1&&a==b/2*2+1)||(a==b-1&&b==a/2*2+1))
                    printf("True\n");
                else printf("False\n");
                continue;
            }
            if(!strcmp(d,w[3])) {
                a=search(n1,n);a=num[a];
                b=search(n2,n);b=num[b];
                while(a>0) {
                    a=a/2;
                    if(a==b) {
                        printf("True\n");
                        break;
                    }
                    if(a==0)
                        printf("False\n");
                }
                continue;
            }
            if(!strcmp(d,w[4])) {
                a=search(n1,n);a=num[a];
                b=search(n2,n);b=num[b];
                while(b>0) {
                    b=b/2;
                    if(a==b) {
                        printf("True\n");
                        break;
                    }
                    if(b==0)
                        printf("False\n");
                }
                continue;
            }
        }
    }
    return 0;
}

搜索更多相关主题的帖子: 文本文件 计算机 Robert 人类学 
2013-09-25 20:03
pauljames
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:千里冰封
威 望:9
帖 子:1555
专家分:10000
注 册:2011-5-8
收藏
得分:10 
代码好长

经常不在线不能及时回复短消息,如有c/单片机/运动控制/数据采集等方面的项目难题可加qq1921826084。
2013-09-25 21:40
lanlong2006
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2013-9-25
收藏
得分:0 
回复 2楼 pauljames
呜呜,感觉很难简化了。。。。。
2013-09-25 21:57
mskeheng
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:48
专家分:179
注 册:2013-3-13
收藏
得分:10 
怎么一点注释都没有呢,这样很难看啊
2013-09-27 22:13
快速回复:(求助)一道关于树的题目,一直错T T
数据加载中...
 
   



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

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