首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

快来看_两小程序几乎一样_但结果不一样_火眼金睛找不同呀,该怎么解决

2012-04-01 
快来看_两小程序几乎一样_但结果不一样_火眼金睛找不同呀各位大神帮忙找一下区别吧,程序A(我写的)超时,程

快来看_两小程序几乎一样_但结果不一样_火眼金睛找不同呀
各位大神帮忙找一下区别吧,程序A(我写的)超时,程序B通过,代码如下:
A:

C/C++ code
#include<stdlib.h>  #include<stdio.h>  struct info{    int num;    struct info *l;    struct info *r;};void deep(struct info *p){    if(p!=NULL)    {        printf("%d",p->num);        if(p->l)        {            printf(" ");            deep(p->l);        }        if(p->r)        {            printf(" ");            deep(p->r);        }    }}main(){    int c,n,root,a,b,i;    struct info t[25];    scanf("%d",&c);    while(c--)    {        for(i=0;i<25;i++)        {            t[i].num=i;            t[i].l=NULL;            t[i].r=NULL;        }            scanf("%d",&n);            scanf("%d",&root);        n--;        while(n--)        {            scanf("%d %d %d",&a,&b,&c);            if(c==0)            {                t[a].l=&t[b];            }            else            t[a].r=&t[b];        }        struct info *q=&t[root];        deep(q);        printf("\n");            }//    system("pause");}

B:
C/C++ code
    #include<stdlib.h>      #include<stdio.h>            typedef struct node      {          int num;          struct node *left;          struct node *right;      }node, *nodelist;            void deep(node *N)      {          if (N != NULL)          {              printf("%d", N->num);              if (N->left)              {                  printf(" ");                  deep(N->left);                          }              if (N->right)              {                  printf(" ");                  deep(N->right);              }          }      }            main()      {          int t, n, r;          int a, b, c;          int i;          node tree[25];              int top;                    scanf("%d", &t);                    while (t--)          {              for (i = 0; i < 25; i++)              {                  tree[i].num = i;                  tree[i].left = tree[i].right =NULL;                      }               scanf("%d", &n);              scanf("%d", &r);              n--;              while (n--)              {                                scanf("%d %d %d", &a, &b, &c);                  if (c)                      tree[a].right = &tree[b];                  else                          tree[a].left = &tree[b];              }              nodelist p = &tree[r];                   deep(p);              printf("\n");          }          // system("pause");      }  

题目如下:
Description
给定一棵有n个结点的二叉树,结点的编号为0~n-1。请你编写程序输出二叉树的前序遍历序列。

Input
输入的第一行是一个正整数t(1 < t < 20),表示有t组测试用例。
对于每组测试用例,第一行是一个整数n(0 < n < 20),表示二叉树结点个数。第二行是一个数r(0≤r≤n-1),二叉树根结点的编号。
后面有n-1行,表示二叉树n-1条边的信息。每行三个数a,b,c,三个数间由空格隔开,其中0≤a,b≤n-1且a≠b, c为0或1。a表示边的起点,b表示边的终点。如果c为0,表示b是a的左儿子;如果c为1,表示b是a的右儿子。

Output
对于每组测试用例输出一行,即:该二叉树的前序遍历序列,两个节点编号之间留一个空格。

Sample Input

2
3
2
2 0 0
2 1 1
7
0
0 1 0
0 2 1
1 3 0
1 4 1
2 5 0
2 6 1

Sample Output

2 0 1
0 1 3 4 2 5 6


[解决办法]
mark,我是来学习的。
[解决办法]
抄的挺好的啊
不过

 while(c--)
{
 跟后面的
scanf("%d %d %d", &a, &b, &c); 用同一个变量


c 就不能到0 了

[解决办法]
对啊,你用变量 c 判断while循环,后面又重新输入改变了 c 的值,循环什么时候结束变成未知了;还有,
程序A里面 if(c == 0),在程序B里面变成 if(c),一个判断假,一个判断真,下面执行的语句却是一样的?

热点排行