快来看_两小程序几乎一样_但结果不一样_火眼金睛找不同呀
各位大神帮忙找一下区别吧,程序A(我写的)超时,程序B通过,代码如下:
A:
#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");} #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"); }
c 就不能到0 了
[解决办法]
对啊,你用变量 c 判断while循环,后面又重新输入改变了 c 的值,循环什么时候结束变成未知了;还有,
程序A里面 if(c == 0),在程序B里面变成 if(c),一个判断假,一个判断真,下面执行的语句却是一样的?